22

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?

ggorlen
  • 44,755
  • 7
  • 76
  • 106
mjmostachetti
  • 378
  • 1
  • 4
  • 14

6 Answers6

14

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.)

Pi Marillion
  • 4,465
  • 1
  • 19
  • 20
10

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:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
8

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

Jay
  • 159
  • 1
  • 8
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.

wkunker
  • 91
  • 1
  • 4
0

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

shorrty
  • 2,431
  • 2
  • 17
  • 9
0

One option 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
ggorlen
  • 44,755
  • 7
  • 76
  • 106