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.
I have a Python 3.6.8 in /usr/local/bin/Python3 and a Python 3.7.6 in /usr/local/bin/Python3.7 on my Macbook [In addition, there is Mac default Python2, which I don't want to change it].
By check which python3
or python3 --version
, I can see the current default version is Python 3.6.8. I'd like to set the default Python3 to Python
3.7.6. How can I do that?
asked May 15, 2020 at 3:42
You can use the "ln" command to create a symbolic link [symlink] to the existing file as:
ln -s -f /usr/local/bin/Python3.7 /usr/local/bin/python
In order to be sure you can close your terminal and then open it again to check the changes default version as before.
python3 --version
answered May 15, 2020 at 4:18
Anya SamadiAnya Samadi
5931 gold badge4 silver badges13 bronze badges