How to install Python3 in Linux

How to install Python3 in Linux
Author: Xiao Hui, Master of Wuhan University in Reading

Blog address: https://blog.csdn.net/xiaohuimary

Github: https://github.com/xiaohui96

Python version
under Linux At present, most systems under Linux have their own version of python2.x, and now the mainstream version of python has reached 3.x. For this we need to install python3.x on our Linux system.

Check Python version


[root@xiaohui ~]# python --version
Python 2.7.5

Install Python 3 Step
1. Use wget to download the Python 3.x installation package

The author downloaded the 3.7.1 version, the rest of the versions can also be downloaded according to their needs


[root@xiaohui ~]# wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1rc2.tgz 

2. Create a folder to store Python3.x


[root@xiaohui ~]# mkdir  /usr/local/python3/

3. Move the compressed package to the created folder and switch to the folder to extract the installation package


[root@xiaohui ~]# mv Python-3.7.1rc2.tgz /usr/local/python3
[root@xiaohui ~]# cd  /usr/local/python3
[root@xiaohui python3]# tar -zxf  Python-3.7.1rc2.tgz 

4. Switch to the unzipped folder


[root@xiaohui python3]# cd ./Python-3.7.1rc2

5. Configure, compile and execute installation


[root@xiaohui Python-3.7.1rc2]# ./configure --with-ssl
[root@xiaohui Python-3.7.1rc2]# make
[root@xiaohui Python-3.7.1rc2]# make install
# 安装成功显示
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-10.0.1 setuptools-39.0.1

There may be some errors in step 5, mainly due to the lack of corresponding dependent packages, which can be solved by installing the corresponding dependent packages through yum. The author encountered three errors.

Error 1 gcc is missing

错误代码
configure: error: no acceptable C compiler found in $PATH
该错误是因为本机缺少gcc编译环境,只需安装gcc即可
# 安装命令
[root@xiaohui Python-3.7.1rc2]# yum install -y gcc

Error 2 zlib is missing


错误代码
zipimport.ZipImportError: can't decompress data; zlib not available
该错误是因为本机缺少zlib解压缩类库,只需安装zlib即可
# 安装命令
[root@xiaohui Python-3.7.1rc2]# yum install -y zlib*

Error 3 libffi-devel is missing


错误代码
ModuleNotFoundError: No module named '_ctypes'
该错误是因为本机缺少libffi-devel包,只需安装此包即可
# 安装命令
[root@xiaohui Python-3.7.1rc2]# yum install -y libffi-devel
注意在安装完缺少的依赖包后,仍需重新运行对应所在的配置、编译和执行安装命令

6. Configure and establish soft links


将python库路径添加到/etc/ld.so.conf配置中
# ld.so.conf文件是存储etc目录下的所有.conf文件
[root@xiaohui Python-3.7.1rc2]# echo "/usr/python/lib" >> /etc/ld.so.conf
[root@xiaohui Python-3.7.1rc2]# ldconfig
# 建立新的软链接至python3.x,原本旧链接无需删除
# 原因在于例如CentOS的yum源是用python2.x编写的,删除可能会出一些错误
[root@xiaohui Python-3.7.1rc2]# ln -s /usr/python/bin/python3 /usr/bin/python3
[root@xiaohui Python-3.7.1rc2]# ln -s /usr/python/bin/pip3 /usr/bin/pip3

After the above steps, the installation of Python 3.x is successfully completed, and we can detect the Python version of the system


[root@xiaohui ~]# python3 --version
Python 3.7.1rc2
# python2.x依旧存在
[root@xiaohui ~]# python2 --version
Python 2.7.5

Use pip3 test

[root@xiaohui Python-3.7.1rc2]# pip3 list
Package    Version 
---------- --------
certifi    2019.3.9
chardet    3.0.4   
future     0.17.1  
idna       2.8     
itchat     1.2.32  
pip        10.0.1  
pypng      0.0.19  
PyQRCode   1.2.1   
requests   2.21.0  
setuptools 39.0.1  
urllib3    1.24.3  
wxpy       0.3.9.8 
You are using pip version 10.0.1, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

The test is successful, python3 has been successfully installed on this Linux system

Guess you like

Origin blog.51cto.com/15067236/2607564