How to make python3.7 default

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


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.

Tags:

Python

Python3