26

I have a few different versions of python on my computer. How do I choose which one is run from my terminal when I type python into the prompt?

0

6 Answers 6

16

Use which to see where your python command resides. Then use ls -l to find out where it really is. Then link the one you want instead. Note that the other installed versions are usually all available by their respective names.

$ which python
/usr/bin/python
$ ls -l /usr/bin/python
lrwxrwxrwx 1 root root 9 Jun 18  2013 /usr/bin/python -> python2.7
$ ls /usr/bin/python*
/usr/bin/python   /usr/bin/python2.7         /usr/bin/python2-config
/usr/bin/python2  /usr/bin/python2.7-config  /usr/bin/python-config
$ sudo ln -sf /usr/bin/python2 /usr/bin/python

Note that this changes which Python version all programs for all users on your computer will probably use! If you only want to change it for yourself. You can alias it by adding a alias python='/usr/bin/python2' line (with python2 replaced by the version you want) to ~/.bashrc in linux or ~/.bash_profile in Mac. (You'll need to restart your terminal session in this case.)

Sign up to request clarification or add additional context in comments.

3 Comments

I don't think you ever want to link python versions like that. See the other answers. I wish I could downvote
@hHhh How can you think you better know what people want than they actually do? Personally, I quite like this way of doing, since I am the only user of my computer and I don't want to bother with virtual environments and use other executables like python-config that need to know which one is the main distribution.
When the question was originally asked, the Linux world worked fundamentally differently. Traditionally these have all been symlinks, and they still are. But nowadays Linux is much more dependent on Python, and even installing a separate Python in parallel in /usr/bin risks causing problems in the first place.
12

py -3 or py -2 etc to choose between versions. Even 32/64 bit versions can be differentiated:

py -2 
py -3.7-32
py -3.7-64

See https://docs.python.org/3/installing/#work-with-multiple-versions-of-python-installed-in-parallel

1 Comment

This used to be specific to Windows, though it is now also available on other OSes.
11

You should have multiple executables for every python version you have. For example, if I type python and hit tab, I see:

$ python
python             python2.5-config   python2.7-config   python3.3          python3.3m-config  pythonw2.7         pythonw3.3-32      
python-config      python2.6          python3            python3.3-32       pythonw            pythonw3           
python2            python2.6-config   python3-32         python3.3-config   pythonw2.5         pythonw3-32        
python2.5          python2.7          python3-config     python3.3m         pythonw2.6         pythonw3.3 

So, if, for example, I want python 2.5 version - I run python2.5.

Also, take a look at virtual environments - it's much easier to manage and switch between multiple python environments with it.

Also see:

Comments

4

To choose which version of python is run when you type 'python' into a terminal, you may want to try using an alias.

For example:

alias python='python2.7'

Would make python2.7 execute when you type 'python' into your terminal.

Comments

3

One option for Debian-based systems (Ubuntu, Mint, etc) is update-alternatives:

$ sudo update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/bin/python3).

  Selection    Path                 Priority   Status
------------------------------------------------------------
  0            /usr/bin/python3.11   2         auto mode
* 1            /usr/bin/python3.10   1         manual mode
  2            /usr/bin/python3.11   2         manual mode

Press <enter> to keep the current choice[*], or type selection number: 0
update-alternatives: using /usr/bin/python3.11 to provide /usr/bin/python3 (python3) in auto mode
$ python3 -V
Python 3.11.0rc1
$ sudo update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/bin/python3).

  Selection    Path                 Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.11   2         auto mode
  1            /usr/bin/python3.10   1         manual mode
  2            /usr/bin/python3.11   2         manual mode

Press <enter> to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/python3.10 to provide /usr/bin/python3 (python3) in manual mode
$ python3 -V
Python 3.10.6

For Red Hat based systems (CentOS, Rocket, etc), you can try alternatives.

Comments

0

Try envirius (universal virtual environments manager), which allows to compile any version of python. Moreover, it allows to create environments with mixed languages.

1 Comment

That's pretty obscure. The more mainstream modern equivalent would probably be pyenv

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.