简体   繁体   中英

Calling specific version of python from PHP

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 :)

你能不能这样做:

exec('/usr/bin/python2.7/python test.py');

another option, you can set path to interpreter in 1st line of script test.py

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

but you need make test.py executable

chmod +x path_to_file/test.py

and run from php as

exec('path_to_file/test.py');

PS be attentive administrators sometimes disable exec function on servers for safety. disable_functions="popen,exec,system,passthru,proc_open,shell_exec" ....

If you can't use full path, try an alias:

alias python='/usr/bin/python2.7'
python --version
Python 2.7.2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM