无法从 PHP exec() 执行 Python 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15594946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Script Failing to Execute from PHP exec()
提问by khan
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.pythen 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?
我有一个简单的 PHP 函数,它应该在调用时执行 Pyton 脚本。我在我的 php 程序中多次尝试过这种函数,但是这次这个函数根本没有执行 python 脚本。当我从命令提示符访问脚本并运行时,python testing.py它会成功执行。我想提到的一件事是这个脚本有一些python的NLTK库的严重实现,并且需要超过20秒来执行和执行其操作(即数据处理和存储到db)。执行中的延迟是导致此问题的原因还是这次我遗漏了其他内容?
function success(){
$mystring = exec('python testing.py');
$mystring;
if(!$mystring){
echo "python exec failed";
}
else{
echo "<br />";
echo "successfully executed!";
}
采纳答案by lenik
you have to use full path for the pythonand for your file. you may find the former from the which pythoncommand, that most likely outputs '/usr/bin/python' and you should already know the latter. so your command would look like this:
您必须为python和 为您的文件使用完整路径。您可能会从which python命令中找到前者,它很可能输出 '/usr/bin/python',您应该已经知道后者。所以你的命令看起来像这样:
$mystring = exec('/usr/bin/python /home/user/testing.py');
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.
并且您应该确保您的 python 脚本具有所有适当的权限,因为您的网络服务器很可能以不同的用户身份运行,因此权限应该是“-rwxrwxr-x”或类似的东西。
回答by Ryan Knopp
try to use exact path to the python program.
尝试使用 python 程序的确切路径。
$mystring = exec('python testing.py');
回答by doitlikejustin
Try removing the $mystring;line
尝试删除该$mystring;行
function success() {
$mystring = exec('python testing.py');
if(!$mystring){
echo "python exec failed";
} else {
echo "<br />";
echo "successfully executed!";
}
}
For testing purposes try:
出于测试目的,请尝试:
function success() {
$mystring = exec('python testing.py', $output);
var_dump($output);
}
回答by Javesh
There is no issue with the exec() or anything.
The problem is that the nltk module is not able to locate the nltk_data directory. For it just locate where the nltk_data is present in your system: usually ~/nltk_data.
Now import add that path when you run the function.
import nltk;
Now, nltk.data.path is a list of locations where to search for the modules.
You can just do nltk.data.path.append("your location/directory");
exec() 或任何东西都没有问题。
问题是 nltk 模块无法定位 nltk_data 目录。因为它只需找到系统中存在 nltk_data 的位置:通常是 ~/nltk_data。
现在在运行该函数时导入添加该路径。
导入 nltk;
现在, nltk.data.path 是搜索模块的位置列表。
你可以做 nltk.data.path.append("your location/directory");

