25

I have installed python3.7 however I am not sure how to make it the default python.

See below:

~/Documents/robosuite$ python3.7
Python 3.7.1 (default, Oct 22 2018, 11:21:55) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
KeyboardInterrupt
>>> 

~/Documents/robosuite$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

I want python3.7 to show up when I use the command python3

4 Answers 4

36

Simple solution is edit .bashrc and put this line:

alias python3=python3.7

Whenever you will write python3 it will replace it with python3.7.

Or you can use command update-alternatives which is preferred i.e:

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2

So here python3.7 will have higher priority then python3.6. Then use:

sudo update-alternatives --config python3

Press the enter key if you are satisfied

3
  • 13
    Trap for young players: I highly recommend using update-alternatives over the alias-approach, as aliases do not take effect in non-shell environments (also non-interactive shells ignore .bashrc). Take a python-file starting with the shebang #!/usr/bin/env python3 as an example: Run with ./myscript.py it would ignore your alias, while python3 myscript.py would run in python 3.7.1 in your case. Other common cases for ambiguities would be invoking commands via ssh, or from inside a shell-script.
    – Folfy
    Jan 24, 2019 at 15:55
  • 1
    Note that the alternatives variant above replaces /usr/bin/python instead of /usr/bin/python3, and will cause issues with Python programs which expect python to be Python 2 (which is the case on Debian and derivatives). See also PEP 394 and the Debian Python policy. Feb 11, 2019 at 6:07
  • On my Ubuntu 18.04 any of the two approaches caused the No module named 'apt_pkg error and the Terminal to not start. I wanted to use python 3.8.1 instead of python 3.6.9.
    – MERose
    Jan 7, 2020 at 20:51
5

I would like to expand on a previous answer as the two approaches given are not equivalent, and one of them can even break the system. The issue is there are two different things one could be trying to do by "changing the default python version".

Quick summary

Add

alias python3=python3.7

to .bashrc.

Do not use update-alternatives to change your default python version, as this will break system applications.

Further details

Bash alias

The first, is that for an interactive shell, one simply wishes to easily open the desired python by writing

$ python

or

$ python3

This is accomplished by adding the line

alias python3=python3.7

to .bashrc. If one is using another shell, add this to the appropriate config file. This also has the advantage that if it causes problems for any reason, one can simply remove the offending line in .bashrc and restart the terminal.

update-alternatives

The second thing one could mean by "changing the default python version", is to change the default python version for all programs, including those not launched from an interactive shell. This is the result of running

$ sudo update-alternatives --config python3

However, if you run this in a clean install of Debian/Ubuntu, you will notice that the command returns

update-alternatives: error: no alternatives for python3

even if you have multiple versions of python 3 installed via apt. There is a very good reason for this.

The problem with the this is that many system applications use python, and depending on the exact distribution, many use python 3. Changing which version is called by the command python3 globally will force these applications to use this version. Although different version of python 3 are largely compatible, there are still features moved and removed between releases. If a system application uses these features, changing python3 to launch a newer version will break the application.

Testing

I created a fresh install of Ubuntu 18.04 in a VM. Adding the bash alias caused no immediate issues.

Using the update-alternatives method caused issues with apt. Specifically, I got

ModuleNotFoundError: No module named 'apt_pkg'

Fixing your system if you ran the update-alternatives method

If we ran update-alternatives and broke apt, we can still fix the system. During my testing, the terminal was still able to be opened. One can go back to the default python by running

$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.X 1000

where 3.X is your systems original python 3 version, and 1000 is just some high priority to make sure it is on top. Then, one can run

$ sudo update-alternatives --config python3

and make sure the original system python is selected. Reboot the system and it will be back to normal.

1

For machines which don't have the update-alternatives command, one can do a quick&dirty solution like

sudo ln -sf `which python3.7` `which python3`

Usually (at least in all cases I've seen), python3/python2/python are only symlinks to the python executable of a specific version. Changing this symlink should change the 'default python'

0
$ sudo su
$ update-alternatives --install /usr/bin/python python /usr/bin/python3 1

... when already python3.7 is installed

2
  • 2
    You might distinguish your answer from unix.stackexchange.com/a/496488/117549 , which also uses update-alternatives, to say why this is better or different.
    – Jeff Schaller
    May 18, 2020 at 23:30
  • This update-alternatives answer is different than the others because it aliases python to python3 whereas the others were aliasing python3 to python3.X.
    – crypdick
    Sep 11, 2020 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.