mjmostachetti
mjmostachetti

Reputation: 716

How to choose which version of python runs from terminal?

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?

Upvotes: 25

Views: 76311

Answers (6)

ggorlen
ggorlen

Reputation: 56855

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.

Upvotes: 3

Jay
Jay

Reputation: 319

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

Upvotes: 11

shorrty
shorrty

Reputation: 2471

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

Upvotes: 0

Pi Marillion
Pi Marillion

Reputation: 4674

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

Upvotes: 16

wkunker
wkunker

Reputation: 91

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.

Upvotes: 4

alecxe
alecxe

Reputation: 473753

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:

Upvotes: 11

Related Questions