0

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
2
  • It might be more platform-agnostic to probe entries from whereis -b python for 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). Commented Jan 2, 2015 at 14:31
  • 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 Commented Jan 2, 2015 at 14:43

1 Answer 1

1
  1. There is an error in your test.py file. Executing the file in a terminal shows the following output:

    bash: ./test.py: /usr/bin/env/python2.7: bad interpreter: Not a directory
    

    To fix this, set the first line to:

    #!/usr/bin/env python2.7
    

    Note the space between env and python2.7.

  2. You override the Python interpreter set in the file by executing /usr/bin/python instead running the script itself. You should be executing the Python script as follows:

    ini_set('display_errors', 'on');
    $output = array();
    $command = dirname(__FILE__) . "/test.py";
    exec($command, $output);
    var_dump($output);
    

    Note: remember to have the execute bit set on the Python file:

    chmod +x test.py
    
Sign up to request clarification or add additional context in comments.

5 Comments

I added this and I am still getting error. Actually the line #!/usr/bin/env doesn't matter.
Did you call the file from php?
I tried the php script and found it doesn't work. As far as i understand this is gonna call default script, but anyways it doesn't work. I am using centos
@Brana: Can you be more specific than "it doesn't work". What is the output from your PHP script?
out is:array(0) { } - so it doesn't work even with python 2.66

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.