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?
6 Answers
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.)
3 Comments
python-config that need to know which one is the main distribution./usr/bin risks causing problems in the first place.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
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
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
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
pyenv