无法从PHP exec()执行Python脚本 [英] Python Script Failing to Execute from PHP exec()
问题描述
我有一个简单的PHP函数,该函数应在调用Pyton脚本时执行.我已经在我的php程序中多次尝试过这种功能,但是这次以某种方式该功能根本没有执行python脚本.当我从命令提示符下访问脚本并运行python testing.py
时,它将成功执行.我要提到的一件事是,该脚本对python的NLTK库进行了一些认真的实现,并且执行和执行其操作(即数据处理并存储到db)需要20多秒钟的时间.执行延迟是否会导致此问题,或者这次我还缺少其他东西吗?
I have a simple PHP function that is supposed to execute a Pyton script when its called. I have tried this sort of function multiple times in my php programs, but somehow this time this function is not executing the python script at all. When I access the script from the command prompt and run python testing.py
then it successfully gets executed. One thing that I want to mention that this script has some serious implementations of python's NLTK library, and takes more than 20 seconds to execute and perform its operations (i.e. data process and storing to db). Is this delay in the execution which is causing this problem or is there something else that I am missing this time?
function success(){
$mystring = exec('python testing.py');
$mystring;
if(!$mystring){
echo "python exec failed";
}
else{
echo "<br />";
echo "successfully executed!";
}
推荐答案
您必须对python
和文件使用完整路径.您可以从which python
命令中找到前者,最有可能输出'/usr/bin/python',并且您应该已经知道了后者.因此您的命令应如下所示:
you have to use full path for the python
and for your file. you may find the former from the which python
command, that most likely outputs '/usr/bin/python' and you should already know the latter. so your command would look like this:
$mystring = exec('/usr/bin/python /home/user/testing.py');
并且您应该确保您的python脚本具有所有适当的权限,因为您的网络服务器很可能以其他用户身份运行,因此权限应为"-rwxrwxr-x"或其他内容.
and you should make sure your python script has all appropriate permissions, because your web-server most probably is running as a different user, so permissions should be "-rwxrwxr-x" or something close.
这篇关于无法从PHP exec()执行Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!