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

How do I make python 2.7.3 default python on my OS. So when i run python -V i should get 2.7.3?

I know it is bad. The alternative is to uninstall python 2.7.3 and I do not know how to do this.

1

The way I see it, you have three options.

  • /usr/bin/python is actually a symlink to the python version that needs to be defaulted. You can simply resymlink that to python2.7 and you should be good on that front. If you don't need python 2.6.6 then why keep it there. You can remove it.

  • Your second option is to alias python=/usr/bin/python2.7 in your bashrc file. This will default to python2.7 at all times.

  • Your third and final option, which is preferable over all the previous options is to use virtualenv.

Virtualenv will create a virtual environment container for you in your current directory. You can do your changes in that environment without affecting your system as other systemwide application might be using python2.6. This is the best and safest option with the least amount of effort involved.

Be Careful: The first two options are viable options but they are not good options as they require an assumption that your system does not need python2.6.

UPDATE If you want an easy fix, at the beginning of your python script you can add the following shebang

#!/usr/bin/env python2.7

This will give you the ability to run that specific script under 2.7. My previous suggestions were on a system-wide basis. But if you only want it on one script, this should suffice. After you do that, make the script executable and simply execute it like any other executable by using ./test.py

To run your script from php, you will have to follow this post that gives you the snippet to be able to do it.

  • Hi, will alias thing work for exec command such exec("/usr/bin/python /test.py", $output). I mean will then be executed python 2.6.6. or 2.7.3? – Brana Jan 2 '15 at 16:49
  • @Brana I updated my answer for you. – Digisec Jan 2 '15 at 16:56
  • Hi, I have tried this but no matter what i use as #!/usr/bin/env python2.7 it is the same. I think that just what I use in exec("/usr/bin/python /test.py", $output) matters. – Brana Jan 2 '15 at 17:10
  • Could be Virtualenv version used with exec? – Brana Jan 2 '15 at 17:16
  • I still don't understand why you want to use exec when you can simply call the script ! – Digisec Jan 2 '15 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.