【发布时间】:2012-02-01 00:50:55
【问题描述】:
我在从 PHP 脚本执行 Python 脚本时遇到问题。我的客户端使用 Bluehost,因此我使用此处描述的 easy_install 方法为 Python 安装了第三方模块(numpy):https://my.bluehost.com/cgi/help/530?step=530
为了演示我的问题,我创建了两个 python 脚本和一个 PHP 脚本。
hello.py 包含:
print "Hello, World!"
hello-numpy.py 包含:
import numpy
print "Hello, World!"
PHP 脚本包含:
Output from exec('python hello.py'): <?php echo exec('python hello.py'); ?><br>
Output from exec('python hello-numpy.py'): <?php echo exec('python hello-numpy.py'); ?><br>
Output from exec('whoami'): <?php echo exec('whoami'); ?>
然后我从 PHP 中得到这个输出:
exec('python hello.py') 的输出:你好,世界!
exec('python hello-numpy.py') 的输出:
exec('whoami') 的输出:venicetw
但是,从 SSH 窗口运行这些脚本会产生以下结果:
# python hello.py
Hello, World!
# python hello-numpy.py
Hello, World!
# whoami
venicetw
当 Python 脚本导入 numpy 时,PHP 似乎没有得到任何输出,但它在 SSH 中运行良好。此外,对于 hello.py,PHP 的返回状态为 0,而 hello-numpy.py 的返回状态为 1。我认为这可能是权限问题,但 PHP 和 SSH 都以“venicetw”用户身份运行。什么会阻止 PHP 和 Apache 从 Python 脚本获取输出?这是我可以与 Bluehost 讨论的事情,还是我应该检查的其他事情?我们正在使用Apache 2.2.21、PHP 5.2.17、Python 2.4.3 和numpy 1.6.0。
更新: SSH 打印以下 Python 路径:
/home8/venicetw/public_html/venicenoise/python
/home8/venicetw/.local/lib/python2.4/site-packages/ogcserver-0.1.0-py2.4.egg
/home8/venicetw/.local/lib/python2.4/site-packages/PIL-1.1.7-py2.4-linux-x86_64.egg
/home8/venicetw/.local/lib/python2.4/site-packages/lxml-2.3.2-py2.4-linux-x86_64.egg
/home8/venicetw/.local/lib/python2.4/site-packages/WebOb-1.2b2-py2.4.egg
/home8/venicetw/.local/lib/python2.4/site-packages/PasteScript-1.7.5-py2.4.egg
/home8/venicetw/.local/lib/python2.4/site-packages/PasteDeploy-1.5.0-py2.4.egg
/home8/venicetw/.local/lib/python2.4/site-packages/Paste-1.7.5.1-py2.4.egg
/home8/venicetw/.local/lib/python2.4/site-packages/numpy-1.6.0-py2.4-linux-x86_64.egg
/home8/venicetw/.local/lib/python2.4/site-packages
/home8/venicetw/.local/lib/python/site-packages
/home8/venicetw/public_html/venicenoise/python
/usr/lib64/python24.zip
/usr/lib64/python2.4
/usr/lib64/python2.4/plat-linux2
/usr/lib64/python2.4/lib-tk
/usr/lib64/python2.4/lib-dynload
/usr/lib64/python2.4/site-packages
/usr/lib64/python2.4/site-packages/Numeric
/usr/lib64/python2.4/site-packages/PIL
/usr/lib64/python2.4/site-packages/gtk-2.0
/usr/lib/python2.4/site-packages
但是 Apache 只打印这些 Python 路径:
/home8/venicetw/public_html/venicenoise/python
/usr/lib64/python24.zip
/usr/lib64/python2.4
/usr/lib64/python2.4/plat-linux2
/usr/lib64/python2.4/lib-tk
/usr/lib64/python2.4/lib-dynload
/usr/lib64/python2.4/site-packages
/usr/lib64/python2.4/site-packages/Numeric
/usr/lib64/python2.4/site-packages/PIL
/usr/lib64/python2.4/site-packages/gtk-2.0
/usr/lib/python2.4/site-packages
解决方案: 通过从 PHP 和 SSH 执行 /usr/bin/env,我能够确定 PHP 缺少安装 numpy 的 Python 路径的环境变量。在这种情况下,通过添加
putenv('PYTHONPATH=/home8/venicetw/.local/lib/python2.4/site-packages:/home8/venicetw/.local/lib/python/site-packages:');
到 PHP 脚本的开头,一切正常。
【问题讨论】: