导入numpy时从php调用python失败 [英] Calling python from php fails when import numpy
问题描述
在从Android发出GET HTTP请求时尝试从php调用python(2.6)脚本,但是如果import numpy在我的脚本中,则会失败,如果将其删除,则该脚本/调用有效.我正在计算MySQL中一个数字字段的FFT频谱,所以我需要numpy(或者是吗?).
Trying to call a python (2.6) script from php when a make a GET HTTP request from Android, but if the import numpy is in my script, I get a failure, if I remove it, the script/call works. I am computing the FFT spectrum of a numeric field in MySQL, so I need numpy (or do I?).
我已经在SO上看到了类似的问题,并且此最好的答案是@ dietrich-epp,但是当我尝试相同的建议时,我的脚本仍然失败.
I've look at similar questions on SO, and this one, answered by @dietrich-epp, is the best one but when I try the same suggestion, my script still fails.
这是我的python脚本的导入部分:
Here is my imports part of my python script:
import os, os.path, sys
import numpy
import string
import math
import MySQLdb
以及我的PHP文件的开头:
And the start of my PHP file:
<?php
putenv('PYTHONPATH=/usr/lib/python2.6/site-packages:');
...
这就是我如何调用我的python脚本:
And this how I call my python script:
$command = "/var/www/html/tremcam/tst0.py localhost baseline@nd.edu 2>&1";
$pid = popen( $command,"r");
while( !feof( $pid ) )
{
#echo fread($pid, 256);
print(json_encode(fread($pid, 256)));
flush();
ob_flush();
usleep(100000);
}
pclose($pid);
我的环境变量的PYTHONPATH设置为:
And my environment variables have the PYTHONPATH set to:
/var/www/html/tremcam/pythons
/usr/lib/python2.6/site-packages
/usr/lib
/usr/lib/python26.zip
/usr/lib/python2.6
/usr/lib/python2.6/plat-linux2
/usr/lib/python2.6/lib-tk
/usr/lib/python2.6/lib-old
/usr/lib/python2.6/lib-dynload
/usr/lib/python2.6/site-packages/gtk-2.0
/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info
/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info
这个(PYTHONPATH=/usr/lib/python2.6/site-packages
)应该指向/usr/lib/python2.6/site-packages/numpy
吗?
我将尝试使用passthru()而不是目前在PHP中使用它的方式.请注意,在我能够将numpy导入为np之后,代码将需要几秒钟的时间来完成,这还会是一个问题吗?
I will try using passthru() instead of the way I currently have it in PHP. Please note, that after I am able to import numpy as np, the code will take several seconds to complete, will that also be an issue?
更新:如果我使用:passthru('/var/www/html/tremcam/tst0.py localhost baseline@nd.edu 2>&1',$result);
我得到退出状态代码139,所以这对我不是很有帮助,您怎么看?
I get exit status code 139, so that isn't very helpful to me, what do you think?
推荐答案
尝试使用
setsebool httpd_tmp_exec on
如果可行,请永久使用
setsebool -P httpd_tmp_exec on
Try with
setsebool httpd_tmp_exec on
if it works make it permanent with
setsebool -P httpd_tmp_exec on
应该是SELinux拒绝Apache的pytonpath目录(是的,如果它仍然不起作用,则应该提供numpy文件夹,但完全没有必要).
It should be SELinux denying Apache your pytonpath directories (and yes you should provide the very numpy folder if it still doesn't work, but it shouldn't be necessary at all).
一种不太干净的方法是在/<your numpy path>/numpy/__init__.py
A slightly less clean approach is to comment this line import ctypeslib
in /<your numpy path>/numpy/__init__.py
我遇到了同样的问题,并且两种方法都运行良好(尽管后者可能会在以后的开发中导致某些不一致之处,所以我建议您先尝试setebool)
I had the same problem and both the approaches worked fine (the latter though may lead to some inconsistency in later developments therefore I suggest you to try setsebool first)
这篇关于导入numpy时从php调用python失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!