CentOS 安装 Python3
安装步骤
一、检查是否有 GCC
gcc --version # 查看gcc版本 # 若没有输出,执行安装命令 yum -y install gcc
二、安装依赖
如果不安装依赖,在安装 Python 的时候会报错
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
yum -y groupinstall "Development tools" yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel yum install -y libffi-devel zlib1g-dev yum install zlib* -y
三、下载python
根据需要下载相关版本的 Python 包,并解压
wget wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tar.xz
tar -xvJf Python-3.7.2.tar.xz
四、创建目录并安装 Python
# 指定安装的路径,不指定的话,安装过程中可能软件所需要的文件复制到其他不同目录,删除软件很不方便,复制软件也不方便 mkdir /usr/local/python3 cd Python-3.7.2 # 配置,可以提高python10%-20%代码运行速度. ./configure --prefix=/usr/local/python3 --enable-optimizations --with-ssl # 编译安装会一起安装 pip、ssl等 make && make install
五、创建软连接
ln -s /usr/local/python3/bin/python3 /usr/local/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/local/bin/pip3
大功告成
python3 -V
pip3 -V
相关报错及解决
安装 Python 时报错
- 报错——zipimport.ZipImportError: can't decompress data; zlib not available Makefile:1099: recipe for target 'install' failed make: *** [install] Error 1
- 原因:缺少依赖
- 解决:
yum -y install zlib1g-dev
- 报错——ModuleNotFoundError: No module named '_ctypes'
- 原因:缺少依赖
- 解决:
yum -y install libffi-devel
pip install 时报错
报错——pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available. Could not fetch URL https:*******: There was a problem confirming the ssl certificate: Can't connect to HTTPS URL because the SSL module is not available. - skipping
原因:在 ./configure 过程中,如果没有加上 –with-ssl 参数时,默认安装的软件涉及到 ssl 的功能不可用,刚好 pip3 过程需要 ssl 模块,而由于没有指定,所以该功能不可用。解决办法是重新对 python 进行编译安装