问题描述
我在从 PHP 脚本执行 Python 脚本时遇到问题.我的客户端使用 Bluehost,因此我使用此处描述的 easy_install 方法为 Python 安装了第三方模块 (numpy):https://my.bluehost.com/cgi/help/530?step=530
I'm having an issue executing a Python script from a PHP script. My client uses Bluehost, so I installed a third party module (numpy) for Python with the easy_install method described here: https://my.bluehost.com/cgi/help/530?step=530
为了演示我的问题,我创建了两个 python 脚本和一个 PHP 脚本.
To demonstrate my issue, I've created two python scripts and a PHP script.
hello.py 包含:
hello.py contains:
print "Hello, World!"
hello-numpy.py 包含:
hello-numpy.py contains:
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 得到这个输出:
I then get this output from PHP:
exec('python hello.py') 的输出:你好,世界!
exec('python hello-numpy.py') 的输出:
exec('whoami') 的输出:venicetw
但是,从 SSH 窗口运行这些脚本会产生以下结果:
However, running these scripts from the SSH window yields the following results:
# python hello.py
Hello, World!
# python hello-numpy.py
Hello, World!
# whoami
venicetw
当 Python 脚本导入 numpy 时,PHP 似乎没有得到任何输出,但它在 SSH 中运行良好.此外,PHP 的 hello.py 返回状态为 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
代码>.
It seems PHP doesn't get any output when the Python script imports numpy, but it works fine from SSH. Furthermore, PHP gets a return status of 0 for hello.py but 1 for hello-numpy.py. I thought it might be a permissions issue, but both PHP and SSH are running as the "venicetw" user. What would prevent PHP and Apache from getting the output from the Python script? Is it something I can discuss with Bluehost, or something else I should check? We're using Apache 2.2.21
, PHP 5.2.17
, Python 2.4.3
, and numpy 1.6.0
.
更新: SSH 打印以下 Python 路径:
Update: SSH prints the following Python paths:
/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 路径:
But Apache only prints these Python paths:
/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 路径的环境变量.在这种情况下,通过添加
Solution: By executing /usr/bin/env from both PHP and SSH, I was able to determine that PHP was missing an environment variable for the Python path where numpy is installed. In this case, by adding
putenv('PYTHONPATH=/home8/venicetw/.local/lib/python2.4/site-packages:/home8/venicetw/.local/lib/python/site-packages:');
到 PHP 脚本的开头,一切都按预期进行.
to the beginning of the PHP script, everything works as expected.
推荐答案
如果 PHP 的返回状态为 1,则表示您运行的进程遇到错误.(非零退出状态通常表示 Unix 风格系统上的错误.几个程序是不同的,例如 diff
.)尝试检查 stderr
由子进程生成以查看那里打印了哪些错误消息.
If PHP gets a return status of 1, that indicates that the process you ran encountered an error. (An nonzero exit status usually indicates an error on Unix style systems. A couple programs are different, e.g., diff
.) Try examining the stderr
produced by the subprocess to see what error messages are printed there.
你可以这样显示stderr
:
Output from exec('python hello-numpy.py'):
<?php echo exec('python hello-numpy.py 2>&1'); ?><br>
2>&1
指示 shell 将 stderr
和 stdout
组合成一个流.您通常不想这样做,但它可以让您轻松查看错误.
The 2>&1
instructs the shell to combine stderr
and stdout
into one stream. You normally don't want to do this, but it can make it easy to see the errors.
更新: 由于您得到的错误是 ImportError: No module named numpy
我们可以尝试查看 Python 的路径.您的系统上可能安装了多个 Python,也有可能 Apache 的环境(根目录等)与您通过 SSH 运行 Python 时获得的环境不同.尝试在两种环境中执行此 Python 脚本:
Update: Since the error you get is ImportError: No module named numpy
we can try looking at Python's paths. It's possible that there are multiple Pythons installed on your system, and it's also possible that Apache's environment (root directory, etc) is different from the environment you get when you run Python over SSH. Try executing this Python script in both environments:
import sys, os
for path in sys.path:
print path
print
print 'Root:', os.readlink('/proc/self/root') # Linux only
这些路径之一应该指向安装 numpy
的位置,并且在 Apache 中运行它时可能会丢失它.或者也许 Apache 进程有一个不同的根目录,它将被 Python 进程继承.
One of those paths should point to where numpy
is installed, and perhaps it is missing when you run it in Apache. Or perhaps the Apache process has a different root directory, which would be inherited by the Python process.
解决方案: 缺少环境变量.查看评论.
Solution: Missing environment variable. See comments.
这篇关于PHP脚本无法从Python脚本获取输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!