centos7同时安装Python2和Python3

目录

  • 1. 背景
  • 2. 安装1.1.1版本的openssl
  • 3. 安装centos依赖包
  • 4. 下载
  • 5. 解压
  • 6. 安装
  • 7. 创建软链接
  • 8. ssl的使用验证
  • 9. 配置阿里云镜像加速

本文适用于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 "", line 1, in 
  File "/root/python-3.10.5/lib/python3.10/ssl.py", line 99, in 
    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

centos7同时安装Python2和Python3_第1张图片

4. 下载

wget https://www.python.org/ftp/python/3.9.1/Python-3.9.1.tgz
centos7同时安装Python2和Python3_第2张图片

5. 解压

tar -zxf Python-3.9.1.tgz
centos7同时安装Python2和Python3_第3张图片

6. 安装

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

检验安装结果:
centos7同时安装Python2和Python3_第4张图片

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命令了
centos7同时安装Python2和Python3_第5张图片
到现在我们就可以同时使用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]#

你可能感兴趣的:(python,python,python2,python3,部署,大数据)