4

I'm setting up a big system relying on python 2.7 being run through php. The call is always something like:

exec('python test.py');

However no matter what I do PHP keeps using python 2.4 for executing my files. Because of the size of the system I can't change in the programming, but will have to make 'python' point directly to python2.7.

By searching around I have reached the conclusion that I should change the php env.

echo getenv("PYTHONPATH"); // NOTHING
echo getenv("PATH"); // /bin:/usr/bin

I can do so through putenv (for example: putenv("PATH=/usr/bin/python2.7:".$_ENV["PATH"]), but php keeps running python 2.4 no matter what I change it to.

Hope somebody out there got a simple solution :)

4

3 回答 3

3

你能不能只这样做:

exec('/usr/bin/python2.7/python test.py');
于 2013-03-10T18:31:29.840 回答
1

另一种选择,您可以在脚本 test.py 的第一行中设置解释器的路径

#!/usr/local/bin/python2.7 

但你需要使 test.py 可执行

chmod +x path_to_file/test.py

并从 php 运行

exec('path_to_file/test.py');

PS 注意管理员有时会禁用服务器上的 exec 功能以确保安全。disable_functions="popen,exec,system,passthru,proc_open,shell_exec" ....

于 2013-03-10T19:53:23.597 回答
0

如果您不能使用完整路径,请尝试使用别名:

alias python='/usr/bin/python2.7'
python --version
Python 2.7.2
于 2013-03-10T18:34:30.103 回答