首页 > 解决方案 > 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'

标签: pythonphpmodulenotfounderror

解决方案


推荐阅读