python3.7.4 安装

​ 本文主要介绍如何在Centos7中通过源码包编译安装方式安装python3.7版本;在安装python3.7之前,切记不要卸载centos自带的python2版本,也不要把python3命名为python, 因为linux的一些自带命令会用到系统自带python2。

  • 下载地址
  1. 官网 https://www.python.org/ftp/python/3.7.4/

  2. http://npm.taobao.org/mirrors/python/

1. 安装依赖包

[root@localhost ~]# yum -y groupinstall "Development tools" 

[root@localhost ~]# yum -y install gcc* zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

# 不安装会在编译安装时报错
[root@localhost ~]# yum install libffi-devel -y

注意: 安装时可能出现的错误

1. 安装时报错 No module named '_ctypes'

3.7版本需要一个新的包libffi-devel,安装此包之后再次进行编译安装即可。

2. 安装时报错 以下

configure: error: in /root/Python-3.7.4':

configure: error: no acceptable C compiler found in $PATH
See ‘config.log’ for more details

缺少 gcc包 安装 yum -y install gcc* 即可

2. 下载,解压安装包

# 很多种方式 ,放在家目录就行

[root@localhost ~]# ls
anaconda-ks.cfg  Python-3.7.4.tgz

# 解压压缩包
[root@localhost ~]# tar xvf Python-3.7.4.tgz

3. 编译安装

# 创建安装python3的路径
[root@localhost ~]# mkdir -p /usr/local/python3

# 进入解压后的 Python目录 进行编译安装
[root@localhost ~]# cd Python-3.7.4

# 1.进行配置编译安装 # --prefix 指定程序存放路径  
[root@localhost Python-3.7.4]# ./configure --prefix=/usr/local/python3

# 2. 编译安装  这步时间有点长,上面依赖包都安装一般不会报错
[root@localhost Python-3.7.4]# make && make install

4. 创建软连接

# 创建软连接 让命令成为系统命令,在任何路径都可以调用

[root@localhost bin]# ln -s /usr/local/python3/bin/python3 /usr/local/bin/python3
[root@localhost bin]# ln -s /usr/local/python3/bin/pip3 /usr/local/bin/pip3

5. 验证是否安装成功

[root@localhost ~]# python3 -V
Python 3.7.4
[root@localhost ~]# pip3 -V
pip 19.0.3 from /usr/local/python3/lib/python3.7/site-packages/pip (python 3.7)

# 提示版本 表示安装成功


# 到此安装结束
附加错误:
  1. pip3 list 报错 TLS/SSL

错误信息
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Collecting virtualenv
Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/virtualenv/
Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/virtualenv/
Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/virtualenv/
Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/virtualenv/
Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/virtualenv/
Could not fetch URL https://pypi.org/simple/virtualenv/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/virtualenv/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
Could not find a version that satisfies the requirement virtualenv (from versions: )
No matching distribution found for virtualenv
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping

原因
yum 安装的openssl 版本都比较低。需要对openssl进行升级,并重新编译python3.7

2 升级openssl

1.下载openssl
[root@localhost ~]# wget https://www.openssl.org/source/openssl-1.1.1a.tar.gz
[root@localhost ~]# tar -xvf openssl-1.1.1a.tar.gz
[root@localhost ~]# cd openssl-1.1.1a

# 2.编译安装
[root@localhost openssl-1.1.1a]# ./config --prefix=/usr/local/openssl no-zlib #不需要zlib
[root@localhost openssl-1.1.1a]# make
[root@localhost openssl-1.1.1a]# make install

# 3.备份原配置
[root@localhost ~]# mv /usr/bin/openssl /usr/bin/openssl.bak
[root@localhost ~]# mv /usr/include/openssl/ /usr/include/openssl.bak

# 4.新版配置
[root@localhost ~]# ln -s /usr/local/openssl/include/openssl /usr/include/openssl
[root@localhost ~]# ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/local/lib64/libssl.so
[root@localhost ~]# ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl

# 5.修改系统配置
## 写入openssl库文件的搜索路径
[root@localhost ~]# echo "/usr/local/openssl/lib" >> /etc/ld.so.conf

## 使修改后的/etc/ld.so.conf生效
[root@localhost ~]# ldconfig -v

# 6.查看openssl版本
[root@localhost ~]# openssl version

openssl version 提示:

/usr/local/openssl/bin/openssl: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or directory

假如你的libssl.so.1.1 文件在/usr/local/openssl/lib/下面,可以这样做

ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/lib64/libssl.so.1.1

ln -s /usr/local/openssl/lib/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1

  1. 重新安装python
[root@localhost ~]# cd  Python-3.7.4 
[root@localhost Python-3.7.4]#  ./configure --prefix=/usr/local/python3 --with-openssl=/usr/local/openssl
[root@localhost Python-3.7.4]#  make && make install
原文地址:https://www.cnblogs.com/aaak/p/13968431.html