Skip to content

Python 3.7.x 安装

leeyoshinari edited this page Apr 1, 2020 · 3 revisions

python3.7及以上版本,出于安全考虑,openssl版本必须是openssl1.0.2以上。目前Linux系统yum源的openssl版本还是openssl1.0.1版本,故必须升级openssl。

查看openssl版本
openssl version
如果Linux系统的openssl版本是openssl1.0.2及以上版本,可跳过安装openssl步骤。

安装openssl

下载openssl
openssl-1.0.2s.tar.gz
openssl-1.1.0k.tar.gz
openssl-1.1.1c.tar.gz

安装openssl
tar -zxvf openssl-1.1.1c.tar.gz
cd openssl-1.1.1c
./configure --prefix=/usr/local/openssl no-zlib
make
make install

创建软连接
在创建软连接之前,可以备份原文件
ln -s /usr/local/openssl/include/openssl /usr/include/openssl
ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/local/lib64/libssl.so
ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl

修改系统配置
#写入openssl库文件的搜索路径
echo "/usr/local/openssl/lib" >> /etc/ld.so.conf
#使修改后的/etc/ld.so.conf生效
ldconfig -v
#再次查看openssl版本
openssl version

安装python

下载python版本
python官网下载相应版本的python源码包,下载完成后解压
tar xvJf Python-3.7.3.tar.xz

安装python
cd Python-3.7.3
./configure --prefix=/usr/local/python37 --with-openssl=/usr/local/openssl --with-ssl=/usr/local/openssl --enable-shared
make
make install
如果_ctypes编译报错或出现ModuleNotFoundError: No module named '_ctypes',原因是缺少依赖包libffi-devel。
yum install libffi-devel -y 即可解决。
然后重新运行makemake install

创建软连接
ln -s /usr/local/python37/bin/python3 /usr/bin/python3
ln -s /usr/local/python37/bin/pip3.7 /usr/bin/pip3

验证是否安装成功

(1)是否可以导入ssl
python3
>>import ssl
(2)是否可以安装第三方包
pip3 install numpy

安装第三方包

pip3 install numpy
如果嫌安装慢,或由于安装慢而连接中断,可尝试使用豆瓣源安装:
pip3 install numpy -i http://pypi.douban.com/simple/
如果使用豆瓣源无法安装,提示需要信任豆瓣源,则:
pip3 install numpy -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com