PHP脚本无法从Python脚本获取输出

时间:2021-12-03 17:08:10

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

我有一个问题,从PHP脚本执行Python脚本。我的客户机使用Bluehost,所以我安装了一个用于Python的第三方模块(numpy),并使用了这里所描述的easy_install方法:https://my.bluehost.com/cgi/help/530?step=530。

To demonstrate my issue, I've created two python scripts and a PHP script.

为了演示我的问题,我创建了两个python脚本和一个PHP脚本。

hello.py contains:

你好。py包含:

print "Hello, World!"

hello-numpy.py contains:

hello-numpy。py包含:

import numpy
print "Hello, World!"

The PHP script contains:

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'); ?>

I then get this output from PHP:

然后我从PHP中得到这个输出:

Output from exec('python hello.py'): Hello, World!
Output from exec('python hello-numpy.py'):
Output from exec('whoami'): venicetw

exec输出('python Hello .py'): Hello, World!exec输出('python hello-numpy.py'): exec输出('whoami'): venicetw

However, running these scripts from the SSH window yields the following results:

但是,从SSH窗口运行这些脚本会得到以下结果:

# python hello.py
Hello, World!
# python hello-numpy.py
Hello, World!
# whoami
venicetw

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.

当Python脚本导入numpy时,PHP似乎没有得到任何输出,但是从SSH它可以很好地工作。此外,PHP的hello返回状态为0。是1,表示hello- numpypy。我认为这可能是一个权限问题,但是PHP和SSH都是以“venicetw”用户的身份运行的。什么会阻止PHP和Apache从Python脚本获取输出?是我可以和Bluehost商量的事,还是其他我应该检查的事?我们使用的是Apache 2.21、PHP 5.2.17、Python 2.4.3和numpy 1.6.0。

Update: SSH prints the following Python paths:

更新: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

But Apache only prints these Python paths:

但是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

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

解决方案:通过从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:');

to the beginning of the PHP script, everything works as expected.

在PHP脚本的开头,一切都按预期运行。

1 个解决方案

#1


5  

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.

如果PHP的返回状态为1,则表明您运行的进程遇到了错误。(非零退出状态通常表示Unix样式系统上的错误。两个程序是不同的,例如diff.)请尝试检查子进程生成的stderr,看看那里打印了什么错误消息。

You can show stderr this way:

你可以这样展示stderr:

Output from exec('python hello-numpy.py'):
    <?php echo exec('python hello-numpy.py 2>&1'); ?><br>

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.

>和1指示shell将stderr和stdout合并到一个流中。您通常不希望这样做,但是这样可以很容易地看到错误。

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:

更新:由于您得到的错误是ImportError:没有名为numpy的模块,我们可以尝试查看Python的路径。您的系统上可能安装了多个Python,而且Apache的环境(根目录等)也可能与您在SSH上运行Python时得到的环境不同。尝试在两个环境中执行这个Python脚本:

import sys, os
for path in sys.path:
    print path
print
print 'Root:', os.readlink('/proc/self/root') # Linux only

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.

其中一条路径应该指向numpy的安装位置,在Apache中运行时可能会丢失它。或者,Apache进程有一个不同的根目录,它将被Python进程继承。

Solution: Missing environment variable. See comments.

解决方案:失踪的环境变量。看到评论。

#1


5  

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.

如果PHP的返回状态为1,则表明您运行的进程遇到了错误。(非零退出状态通常表示Unix样式系统上的错误。两个程序是不同的,例如diff.)请尝试检查子进程生成的stderr,看看那里打印了什么错误消息。

You can show stderr this way:

你可以这样展示stderr:

Output from exec('python hello-numpy.py'):
    <?php echo exec('python hello-numpy.py 2>&1'); ?><br>

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.

>和1指示shell将stderr和stdout合并到一个流中。您通常不希望这样做,但是这样可以很容易地看到错误。

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:

更新:由于您得到的错误是ImportError:没有名为numpy的模块,我们可以尝试查看Python的路径。您的系统上可能安装了多个Python,而且Apache的环境(根目录等)也可能与您在SSH上运行Python时得到的环境不同。尝试在两个环境中执行这个Python脚本:

import sys, os
for path in sys.path:
    print path
print
print 'Root:', os.readlink('/proc/self/root') # Linux only

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.

其中一条路径应该指向numpy的安装位置,在Apache中运行时可能会丢失它。或者,Apache进程有一个不同的根目录,它将被Python进程继承。

Solution: Missing environment variable. See comments.

解决方案:失踪的环境变量。看到评论。