How to switch the default Python version of Linux
This article mainly introduces Linux how to switch the default Python version of the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this Linux how to switch the default Python version of the article will have a harvest, let's take a look.
You can use the ls command to see which Python binaries are available on your system as follows:
$ls / usr/bin/python*/usr/bin/python / usr/bin/python2 / usr/bin/python2.7 / usr/bin/python3 / usr/bin/python3.4 / usr/bin/python3.4m / usr/bin/python3m
Then execute the following command to view the default Python version information:
$python-- versionPython 2.7.8
Modify the Python version based on the user:
To modify the Python version for a particular user, simply create an alias (alias) under its home directory. Open the user's ~ /. Bashrc file and add new alias information to modify the default version of Python.
Alias python='/usr/bin/python3.4'
Once you have done this, log in again or reload the .bashrc file for the operation to take effect.
$. ~ / .bashrc
Check the current version of Python.
$python-- versionPython 3.4.2
Modify the Python version based on the system:
We can use update-alternatives to change the Python version for the entire system. To log in as root, first list all available python alternative version information:
# update-alternatives-- list pythonupdate-alternatives: error: no alternatives for python
If the error message shown above appears, the alternative version of Python has not been recognized by the update-alternatives command. To solve this problem, we need to update the alternative list to include python2.7 and python3.4.
# update-alternatives-- install / usr/bin/python python / usr/bin/python2.7 1update-alternatives: using / usr/bin/python2.7 to provide / usr/bin/python (python) in auto mode# update-alternatives-- install / usr/bin/python python / usr/bin/python3.4 2update-alternatives: using / usr/bin/python3.4 to provide / usr/bin/python (python) in auto mode
The * *-- install option uses several parameters to create symbolic links. * * the last parameter specifies the priority of this option, and if we do not manually set the alternative, then the option with the highest priority will be selected. In this example, we set the priority for / usr/bin/python3.4 to 2, so the update-alternatives command automatically sets it to the default Python version.
# python-versionPython 3.4.2
Next, we list the available alternative versions of Python again.
# update-alternatives-list python/usr/bin/python2.7/usr/bin/python3.4
From now on, we can use the command below to switch between the listed alternative versions of Python at any time.
# update-alternatives-- config python# python-- versionPython 2.7.8
Remove the alternate version:
Once an alternative version of Python no longer exists on our system, we can remove it from the update-alternatives list. For example, we can remove the python2.7 version from the list.
# update-alternatives-remove python / usr/bin/python2.7update-alternatives: removing manually selected alternative-switching python to auto modeupdate-alternatives: using / usr/bin/python3.4 to provide / usr/bin/python (python) in auto mode this article on "how to switch the default Python version of Linux" ends here, thank you for reading! I believe you all have a certain understanding of the knowledge of "how to switch the default Python version of Linux". If you want to learn more, you are welcome to follow the industry information channel.