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

3 Answers 3

3

Could you not just do this instead:

exec('/usr/bin/python2.7/python test.py');
Sign up to request clarification or add additional context in comments.

3 Comments

Python is exec from around 100 different files so I will have to change the system.
@Bornakke It should be pretty straightforward to build a shell script to search and replace your expressions, optionally asking for your approval before changing them.
Can't do. The project is part of a big git project and is already running on other servers with the current python call. What I'm looking for is a way to change PHP enviromental path.
1

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');

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

4 Comments

Python is exec from around 100 different files so I will have to change the system which can't be done since the project is part of a big git project and is already running on other servers with the current python call. What I'm looking for is a way to change PHP enviromental path
i'm not sure, but i read about option python.append_path for php.ini php.net/manual/en/ini.list.php or find and replace your exec(...) command like proposed
The python.append_path sounds like a good solution, but can i change that without changing the php.ini, and if yes -how?
if you using apache and mod_python installed you can set PythonPath in htaccess look here , or google such solution for nginx =)) although personally I would not install extension just for this thing, find and replace 10-20 strings of you code easiest solution
0

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

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

3 Comments

Alias works in the terminal and when I run python here I get the right version. But Alias doesn't change the way PHP make use of python.
Did you try to set alias for user (nobody or www-data) who runs web server?
yep. But it is already the same user. But PHP wouldn't use the alias, since the env. path is defined inside php.ini.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.