I want to execute python from php and I do have a script that works fine for default python interpreter. I have centos with default python 2.6.6 which is installed at /usr/bin/python and python 2.7.3 which is installed at /usr/local/bin/python2.7. You can see what is the default python version:
[root@me ~]# python -V
Python 2.6.6
[root@me ~]# python2.7 -V
Python 2.7.3
Now I want to call a simple python script from php. It should be executed in python 2.7.3 and not in python 2.6.6. But I only managed to do it with the default version of python.
PHP(test.php):
<?php
ini_set('display_errors', 'on');
$output = array();
$dir = dirname(__FILE__);
$command = "/usr/bin/python ". $dir . "/test.py";
$command1 = "/usr/local/lib/python2.7/ ". $dir . "/test.py";
$output = null;
exec($command, $output);
var_dump($output);
?>
Python(test.py):
#!/usr/bin/env/python2.7
import sys
print(sys.version)
OUTPUT (once I run test.php in browser):
array(2) { [0]=> string(41) "2.6.6 (r266:84292, Jan 22 2014, 09:42:36)" [1]=>
string(38) "[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)]" }
So when I use :
exec($command, $output);
everything is fine and it shows the correct version of python (2.6.6)
However when I run test.php with:
exec($command1, $output);
I get these results (no results):
array(0) { }
Therefore there are no results, so i wonder if the python sees at all the installation 2.7.3
From command line 2.7.3 is available - for example:
[root@me ~]# python2.7
Python 2.7.3 (default, Jan 2 2015, 00:56:13)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "it works"
it works
So how do i fix this?
Edit:
The result of whereis -b python :python:
/usr/bin/python /usr/bin/python2.6 /usr/lib/python2.6 /usr/lib64/python2.6
/usr/local/bin/python2.7-config /usr/local/bin/python2.7 /usr/local/lib/python2.7
/usr/include/python2.6
whereis -b pythonfor the right version (might need some heuristics). Better yet, of course, would be making your script compatible to all contemporary versions (such as 2.7 + 3.2 and beyond).