Switch to use different versions of python in ubuntu

introduction

Sometimes we have to use different versions of python environment in the same ubuntu. The introduction of this article is that several different versions of python can be installed on ubuntu at the same time, and then you can specify the python version you want to use at any time.

step

Check current python version

$ python3 --version
python 3.6.8

My version is 3.6.8

Suppose I want to install python3.7

$ sudo apt update -y
$ sudo apt install python3.7

Add both versions of python to update-alternatives

$ 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

Flexibility to choose the python version you want to use

  1. Open the options menu for the python version:

$ sudo update-alternatives --config python3

After entering the command, the menu prompts:

Enter 1 and 2 to replace python3.6 or python3.7.

Enter 2 here as an example, and then use the `python3 --version` command to query the current python version to check whether the switch is successful.

If you want to permanently set a new version of python as the default python to use

The symbol table of the python soft link can be changed by the following command:

$ sudo rm /usr/bin/python3
$ sudo ln -s python3.7 /usr/bin/python3

In this way, the default python version used by the device will be replaced with python3.7 (generally not recommended, because replacing the system's python version may cause some system components to fail to work properly).

Summarize

1) The example describes how to use two different versions of python in ubuntu, and how to flexibly switch and update the python version.

Guess you like

Origin blog.csdn.net/wangyx1234/article/details/129130357