Preface : For now ubuntu 20.04 The system will bring its own python, Based on actual development work requirements , Sometimes you need to use python2 , Sometimes you need to use python3; The two libraries involved have their own calls . Like databases xlrd, If using python3 Installed , Then python2 Not available in ;
Based on this question, explain ,python2 and python3 Installation and use problems ;
1、 install python
python2 Installation command (2.7):
sudo apt-get install python2
2、 View installed python
command :
find /usr/bin/ -name python*
ls /usr/local/lib
3、 Configure the system default python edition
(1) see python Substitute version information , If you are prompted “error”, Indicates... In the current system python The replacement version has not been installed ;
command :
sudo update-alternatives --list python
(2) To configure python
take /usr/bin/python2.7 The priority is set to 1, command :
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
take /usr/bin/python3.8 The priority of is set to 2, command :
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 2
because python3 Is greater than python2 The priority of the , The system defaults to python The version is python3
(3) Review python Substitute version information , command :
sudo update-alternatives --list python
4、 see python The default version
command :
python -V
or
python --version
5、 Switch python edition
command :
sudo update-alternatives --config python
According to the picture above , Enter the corresponding number , You can set the system default python edition ; Such as the input “1”, enter , By default python Will be python2, Enter as shown below :
thus ,python2 and python3 At the same time, the installation is completed ;
6、 install pip
(1)python3 install pip, command :
sudo apt install python3-pip
(2)python2 install pip, command :
sudo apt install python-pip
python2 install pip The command of will prompt that the software package cannot be found , Here's the picture :
At this point, you need to manually download and install ;
First installation curl Tools , command :
sudo apt-get install curl
Then download python2.7 Of pip Script , command :
sudo curl https://bootstrap.pypa.io/pip/2.7/get-pip.py -o get-pip.py
or
sudo curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py
Downloading scripts may break , Or failure ; You can open the corresponding web page to download ;
Finally, run the script , install python2 Of pip, command :
sudo python get-pip.py
7、 To install xlrd For example
python3 Lower installation xlrd, command :
sudo update-alternatives --config python # Switch python Version is python3
python --version # View the current default python edition
pip --version # View the current pip Default python edition
pip install xlrd==1.2.0 # install xlrd library
You can also use
pip install xlrd
However, the installed version may be too high , Make it unusable . So it is recommended to install 1.2.0 Version can ;
python2 Lower installation xlrd, command :
sudo update-alternatives --config python # Switch python Version is python2
python --version # View the current default python edition
pip --version # View the current pip Default python edition
pip install xlrd==1.2.0 # install xlrd library
You can also use
pip install xlrd
However, the installed version may be too high , Make it unusable . So it is recommended to install 1.2.0 Version can ;
summary , thus python2 and python3 Use alternately , And the corresponding library installation and explanation are completed , If there is any deficiency 、 What's missing , Please also leave a message and give feedback , thank you .