The default version of Python is switched to alternate version under Linux

Source: Internet
Author: User

This article is linked from http://www.myhack58.com/Article/48/66/2016/71806.htm

When you install Debian Linux, it is possible for the installation process to provide you with multiple versions of Python available at the same time, so there will be more than one Python executable binary file in the system. You can use the LS command to see which Python binaries are available in your system, as in the following ways.

    1. $ ls/usr/bin/python*
    2. /usr/bin/python/usr/bin/python2/usr/bin/python2.7/usr/bin/python3/usr/bin/python3.4/usr/bin/python3.4m/usr/bin /python3m

Execute the following command to view the default Python version information:

    1. $ python--version
    2. Python 2.7.8
1. Modify Python version based on User:

To modify the Python version for a particular user, simply create an alias (alias) in its home directory. Open the user's ~/.BASHRC file and add new alias information to modify the Python version that is used by default.

    1. Alias python= '/usr/bin/python3.4 '

Once you have done this, log back in or reload the. bashrc file for the operation to take effect.

    1. $ . ~/.bashrc

Check the current Python version.

    1. $ python--version
    2. Python 3.4.2
2. Modify the Python version at the system level

We can use Update-alternatives to change the Python version for the entire system. Log in as root and first list all available Python alternative version information:

    1. # update-alternatives--list python
    2. Update-alternatives:error:no Alternatives for Python

If the error message shown above is present, the alternate version of Python is not yet recognized by the update-alternatives command. To solve this problem, we need to update the alternate list and put python2.7 and python3.4 into it.

    1. # update-alternatives--install/usr/bin/python python/usr/bin/python2.7 1
    2. update-alternatives:using/usr/bin/python2.7 to Provide/usr/bin/python (python) in auto mode
    3. # update-alternatives--install/usr/bin/python python/usr/bin/python3.4 2
    4. update-alternatives:using/usr/bin/python3.4 to Provide/usr/bin/python (python) in auto mode

The--install option uses multiple parameters to create symbolic links. The last parameter specifies the priority of this option, and if we do not manually set the override option, 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 as the default Python version.

    1. # python--version
    2. Python 3.4.2

Next, we list the available Python replacement versions again.

    1. # update-alternatives--list python
    2. /usr/bin/python2.7
    3. /usr/bin/python3.4

From now on, we can use the commands below to switch freely in the list of Python alternatives at any time.

    1. # update-alternatives--config python

    1. # python--version
    2. Python 2.7.8
3, remove the replacement version

Once we no longer have an alternate version of Python in our system, we can remove it from the Update-alternatives list. For example, we can remove the python2.7 version from the list.

    1. # update-alternatives--remove python/usr/bin/python2.7
    2. update-alternatives:removing manually selected alternative-switching python to auto mode
    3. update-alternatives:using/usr/bin/python3.4 to Provide/usr/bin/python (python) in auto mode

The default version of Python is switched to alternate version under Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.