Problems encountered when ubuntu16.04 switches the default python version and uses pip to install packages

1. Switch the default Python version

Use the following command to view the version number information that can be replaced:

update-alternatives --list python

If an error message appears after executing the command

update-alternatives: error: no alternatives for python

Indicates that the alternative version of Python has not been installed, execute the following command to install it:

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2

After execution, you will find that the default Python version of the system is Python3.5. Re-execute
update-alternatives --list python, and the output is as follows:
insert image description here
At this time, we can switch the Python version:

sudo update-alternatives --config python

The output is as follows:
insert image description here
Enter the corresponding serial number to select the default version.

2. Problems encountered when executing pip

Install pip:

sudo apt-get install python-pip(Python2安装)
sudo apt-get install python3-pip(Python3安装)

Check the pip version number:

pip --version

1. The following problems may occur:

Traceback (most recent call last): File “/usr/local/bin/pip”, line7, in module
from pip._internal import main
ImportError: No module named _internal

Forcibly reinstall pip to solve the problem:

wget https://bootstrap.pypa.io/get-pip.py  --no-check-certificate
sudo python get-pip.py --force-reinstall(重装Python2)
sudo python3 get-pip.py --force-reinstall(重装Python3)

2. There is a problem after upgrading pip

Upgrade the pip version:

sudo pip install --upgrade pip

If you encounter an error during use after upgrading:

ImportError: cannot import name main

Solution:

sudo gedit /usr/bin/pip

Will

from pip import main
if __name__ == '__main__':
    sys.exit(main())

changed to

from pip import __main__
if __name__ == '__main__':
    sys.exit(__main__._main())

Guess you like

Origin blog.csdn.net/qq_42938987/article/details/83990333