无法从PHP(apache2)执行python脚本。

问题描述 投票:0回答:1

我想在我的macos上使用apache2从PHP文件中执行Python脚本。我可以执行简单的Python脚本,如。

从PHP:

$result = shell_exec("python /Library/…./example.py '$name' ‘$email’ ”);
var_dump($result);

到Python

import sys

x = sys.argv[1]
y = sys.argv[2]

print("name: " + x + "<br>")
print("email: " + y + "<br>")

而输出是。

enter image description here

但当我试图导入像.NET这样的包时

import openpyxl
import numpy as np
import matplotlib.pyplot as plt

我得到的是

enter image description here

我的问题是,有人知道吗?

1 - 我怎样才能使这些(和其他)包工作?

2 – shell_exec目前执行的是python2,如何添加python3? (如果我写的是python3而不是python就不行了)

python php apache2 shell-exec
1个回答
0
投票

如果以后有人遇到同样的问题,这里是我发现的。

通过shell_exec执行一些不同的命令,我发现它的行为与我的终端不同。

enter image description here

于是我发现,当我通过shell_exec执行python3时,它没有指向正确的地址,那么我所要做的就是传递正确的地址。

$result = shell_exec("/Library/Frameworks/Python.framework/Versions/3.7/bin/python example.py '$name' ‘$email’ ”);
var_dump($result);

现在脚本就能用了:)

© www.soinside.com 2019 - 2024. All rights reserved.