Alex Kinman
Alex Kinman

Reputation: 2615

How to select which version of python I am running on Linux?

The version of Linux I am working on has python 2.6 by default, and we installed 2.7 on it in a separate folder.

If I want to run a .py script, how do I tell it to use 2.7 instead of the default?

Upvotes: 6

Views: 16515

Answers (4)

MariusSiuram
MariusSiuram

Reputation: 3644

Sorry for "stealing" answers, but I feel that there is a little bit of chaos here.

There are different ways to achieve that, it depends on at what level the decision is taken.

System Level

[Credit to @Kenly]

Use the update-alternatives (if you are in Debian or Debian-derivative such as Ubuntu) or alternatives (for Red Hat ecosystem) command, provided by the OS. Specifically, use

$ update-alternatives --config python

or

$ alternatives --config python

(with sudo if necessary). You will be able to choose the specific version.

Result: Once you do this, everything that uses "python" will use the python2.7 binary. This will happen in the whole system, for all users.

User Level

This is a little bit trickier. Credit to @TheFlyingProgrammer

The basic approach would be to update your PATH or create an alias for your shell (typically by changing the shell's startup file such as .bashrc, .zshrc etc). However, a remaining problem is if you are relying on the "shebang" of the file:

#!/usr/bin/python
<python code here>

this kind of file will be unaffected of your changes. However:

#!/usr/bin/env python
<python code here>

or executions like

$ python name_of_script.py

will use the interpreter of choice (the one preferred by your shell).

Result: The user whose PATH or alias settings dictate a specific version of Python will get to use that specific version. But some shebangs will behave differently. In other words, this is a bit trickier. So, be cautious.

Script Level

[Credit to @Anony-Mousse]

The "shebang" approach, modifying the first line. The idea is using the full path in the first line of the Python source file:

#!/usr/bin/python2.7 
<python code here>

You can use python, python2, or python2.7 and you will be more or less specific in the version. The problem would be if you want it to be portable. A similar approach would be to use full specification of version but without path:

#!/usr/bin/env python2.7
<python code here>

Note that if the PATH is not correctly set, this won't work. This gives some power to the user (when setting the PATH). You can, for instance, choose #!/usr/bin/env python2 to force some Python 2.x flavour, but maybe the specific binary will change from user to user.

In addition to that, keep in mind that if you plan to use virtual environments, using /usr/bin/env python is advisable (if I'm not mistaken).

Obviously, the Python version you want to use needs to exist. If there is no file named /usr/bin/python2.7 then of course you cannot run that.

Result: Well, it depends whether you use the env binary or not. But, in any case, you are putting the semantic in the file, which makes sense in many cases (e.g. if there is an incompatibility, it is at the script level).

Execution Level

[Credit to @Prune]

This is the simplest approach:

$ /path/to/your/python/bin/python2.7 my_script.py

You change, for this specific execution, which binary will be using (the Python interpreter ignores the shebang, because it is a comment).

Result: You override all other choices by cherry-picking your Python binary. Very good approach to test behaviour, but not very maintainable or shareable.

Upvotes: 3

Has QUIT--Anony-Mousse
Has QUIT--Anony-Mousse

Reputation: 77485

If you require a particular version, use the full path.

If you have e.g. python2.7 and python3.4 installed (and this is very common, as they are not fully compatible):

A script with

#!/usr/bin/python

will usually be running the latest version of python2 because of comparibility reasons. **You should avoid overriding what /usr/bin/python points to, to not break your system. Some apps will require this to point to a compatible version.

Instead, use

#!/usr/bin/python3

to use the latest python 3

#!/usr/bin/python2.7

to require python2.7

If you manually installed python (why? use the packages, that is much smarter because of automatic upgrades) then use the full path!

~/my-python/bin/python myscript.py

or if you start your script with the shebang:

#!/home/whatever/my-python/bin/python

so you can +x your script and do simply

./myscript.py

or make yourself an alias such as py if you are lazy to type.

Upvotes: 1

Eliot S Winchell
Eliot S Winchell

Reputation: 368

open ~/.bashrc with a text editor, and add alias python=/usr/local/bin/python2.7

In case you're using Cent OS as your linux distribution, you will notice that sudo python <file.py> doesn't work. That's because sudo doesn't have /usr/local/bin as a secure path. Open /etc/sudoers with a text editor, and you should see

#
# Adding HOME to env_keep may enable a user to run unrestricted
# commands via sudo.
#
# Defaults   env_keep += "HOME"

Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin

Add :/usr/local/bin to that secure_path and save.

Upvotes: 0

Kenly
Kenly

Reputation: 26748

Use update-alternatives --config python and shoose python2.7 from choices.
If you need to remove it use update-alternatives --remove python /usr/bin/python2.7.

Upvotes: 3

Related Questions