python - PHP:调用 python 脚本会引发 ModuleNotFoundError,从终端调用是否正常?
问题描述
我有一个脚本,让我们调用它/home/pi/somescript.py
,当我调用它时,/bin/python3.7 /home/pi/somescript.py
一切正常。
现在我试图从一个 php 脚本调用exec('/bin/python3.7 /home/pi/somescript.py', $output, $ret_code);
. 然而,这会抛出一个ModuleNotFoundError
.
但是当我运行交互式 PHP shell 并将上面exec
的内容粘贴到那里时,它也可以工作。我错过了什么吗?这可能是什么原因?我该如何解决?
python脚本的相关部分:
import sys
sys.stderr = sys.stdout # so I get the stacktrace in the $output of the php file
from iso3166 import countries # <- error here
# ...
我正在运行lighttpd
服务器。PHP 文件位于/var/www/html/info.php
,我通过浏览器访问它与http://raspberrypi:8080/info.php
PHP 文件的相关部分:
exec('/bin/python3.7 /home/pi/somescript.py', $output, $ret_code);
$data = ["out" => $output];
header('Content-Type: application/json');
echo json_encode($data);
// this way I get the output when in the browser
我得到的堆栈跟踪:
Traceback (most recent call last):
File "/home/pi/somescript.py", line 6, in <module>
from iso3166 import countries
ModuleNotFoundError: No module named 'iso3166'
解决方案
推荐阅读
- amazon-web-services - 用于在 AWS EB 容器服务上部署的 Gitlab Registry 凭证
- ibm-watson - 可供下载的字典的位置
- .net - TPL 数据流与普通信号量
- ios - UIEdgeInsetsInsetRect' 已被实例方法 'CGRect.inset(by:)
- python - 在 docker 容器之间共享 gensim 的 KeyedVectors 对象的内存
- python - 随着时间的推移,策略梯度算法变得更糟
- wifi - 这是 802.11 数据中的哪一帧?
- docker - Gitlab CI - 使用共享运行器构建 Docker 映像(无法连接到 Docker 守护程序)
- ios - 从不同的线程写不同的文件是不是很糟糕?
- jquery - Kendo datepicker:区分空值和无效值