Several ways to set Python3 as the default Python version in Linux

Method 1, through the alias command, this method is user-level modification

First check the python3 version in your computer:

python python3 --version

In your personal home directory, open the .bashrc file, open it as

python sudo gedit ~/.bashrc

If the gedit text editor is not installed, use the following command to install it

sudo apt install gedit

Or use vim, nano, etc. After opening, enter the following:

alias python='/usr/bin/python3'

Then log out or enter the following command to make the command take effect immediately

source ~/.bashrc

Method 2, through the soft link command ln, this method is a system-level modification

First delete the default Python soft link:

sudo rm -rf /usr/bin/python

Then create a new symlink pointing to the desired Python version:

sudo ln -s /usr/bin/python3 /usr/bin/python

If you want to revert back to the original python2.7, just

sudo rm -rf /usr/bin/python`
`sudo ln -s /usr/bin/ptyhon2.7 /usr/bin/python

Note that the basic usage of the ln command is

ln -s soft link created by the target that needs to be linked

Method 3 is based on the update-alternatives command. This method is a system-level modification.

Just execute the following two commands:

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150

If you need to change back to the python2 default, enter:

sudo update-alternatives --config python

complete.

Guess you like

Origin blog.csdn.net/weixin_35770067/article/details/131063636