centos7同时安装Python2和Python3

本文适用于Python 3.10.5版本,已经过测试

1. 背景

我们通过命令python -V查看安装的版本为python2

python版本图
通过which python查看python可执行文件的位置

python可执行文件位置图
centos默认只安装了python2,现在我们安装python3

2. 安装1.1.1版本的openssl

因为python的ssl模块需要openssl 1.1.1,所以我们需要卸载旧版本的openssl,再手动安装新的openssl

不然会出现如下问题:

问题一:

[spark@spark2 python3]# pip3 install --upgrade pip
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Requirement already satisfied: pip in /home/spark/bulut/python3/python3/lib/python3.10/site-packages (22.0.4)
WARNING: 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/pip/
WARNING: 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/pip/
WARNING: 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/pip/
WARNING: 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/pip/
WARNING: 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/pip/
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
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
WARNING: 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
[spark@spark2 python3]#

问题二:

>>> import ssl
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/root/python-3.10.5/lib/python3.10/ssl.py", line 99, in <module>
    import _ssl             # if we can't import it, let the error propagate
ModuleNotFoundError: No module named '_ssl'
>>> 

卸载旧版本的openssl

[spark@spark2 ~]# rpm -qa | grep openssl
openssl-libs-1.0.2k-25.el7_9.x86_64
openssl-1.0.2k-25.el7_9.x86_64
[spark@spark2 ~]#
[spark@spark2 ~]# yum -y remove openssl-1.0.2k-25.el7_9.x86_64

手动安装1.1.1版本的openssl

[spark@spark2 ~]# wget http://www.openssl.org/source/openssl-1.1.1.tar.gz
[spark@spark2 ~]#
[spark@spark2 ~]# tar -zxvf openssl-1.1.1.tar.gz
[spark@spark2 ~]#
[spark@spark2 ~]# cd openssl-1.1.1
[spark@spark2 openssl-1.1.1]#
[spark@spark2 openssl-1.1.1]# ./config --prefix=/usr/local/openssl
Operating system: x86_64-whatever-linux2
You need Perl 5.
[spark@spark2 openssl-1.1.1]# yum -y install perl
[spark@spark2 openssl-1.1.1]#
[spark@spark2 openssl-1.1.1]# ./config --prefix=/usr/local/openssl
[spark@spark2 openssl-1.1.1]#
[spark@spark2 openssl-1.1.1]# make && make install
[spark@spark2 openssl-1.1.1]#
[spark@spark2 openssl-1.1.1]# ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
[spark@spark2 openssl-1.1.1]# ln -s /usr/local/openssl/include/openssl /usr/include/openssl
[spark@spark2 openssl-1.1.1]# ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/lib64/libssl.so.1.1
[spark@spark2 openssl-1.1.1]# ln -s /usr/local/openssl/lib/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1
[spark@spark2 openssl-1.1.1]#
[spark@spark2 openssl-1.1.1]# openssl version
OpenSSL 1.1.1  11 Sep 2018
[spark@spark2 openssl-1.1.1]#

3. 安装centos依赖包

yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make libffi-devel

yum install效果图

4. 下载

wget https://www.python.org/ftp/python/3.9.1/Python-3.9.1.tgz
python下载图

5. 解压

tar -zxf Python-3.9.1.tgz
解压效果图

6. 安装

  1. cd Python-3.9.1
  2. ./configure --prefix=/home/spark/bulut/python3/python3 --with-openssl=/usr/local/openssl
  3. make && make install

检验安装结果:
Python检验安装结果图

7. 创建软链接

ln -s /home/spark/bulut/python3/python3/bin/python3 /usr/bin/python3
ln -s /home/spark/bulut/python3/python3/bin/pip3 /usr/bin/pip3

现在就可以直接使用python3pip3命令了
软链接结果图
到现在我们就可以同时使用Python2和Python3了

8. ssl的使用验证

[spark@spark2 bulut]# pip3 install --upgrade pip
[spark@spark2 bulut]#
[spark@spark2 bulut]# python3
Python 3.9.1 (default, Dec  8 2020, 11:26:26)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> import ssl
>>>

9. 配置阿里云镜像加速

[spark@spark2 python3]# mkdir ~/.pip
[spark@spark2 python3]#
[spark@spark2 python3]# cat ~/.pip/pip.conf
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/

[install]
trusted-host = mirrors.aliyun.com

[spark@spark2 python3]#
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值