CentOS 7编译安装python3

CentOS 7编译安装 python3

CentOS上自带了python2的环境,且yum依赖。想在CentOS上安装python3环境。还需要编译安装,且要解决环境冲突,python3的依赖。中间也遇到很多问题,比如编译报错,openssl相关的https问题。好在都已经解决,操作记录备忘。

更新centos,以及安装依赖

编译安装python3所需要的依赖,可以参考以下:

1
2
3
4
5
6
7
yum check-update
yum update
yum install epel-release
yum groupinstall 'Development Tools'
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel bzip2-devel libff-devel perl perl-devel perl-ExtUtils-Embed libxslt libxslt-devel libxml2-devel libxml2 gd gd-devel GeoIP GeoIP-devel
# 安装了以上依赖,可能还不够,可以安装openssl11,可能有用,实际后面我是通过编译安装openssl11,编译时指定openssl的路径后,才没有https报错的问题。
yum install openssl11 openssl11-devel openssl-libs

更新GCC

编译中还遇到过编译报错的情况,尝试编译安装gcc升级后,python3编译没有报错了。操作如下:

1、安装scl源

1
yum install centos-release-scl

2、安装gcc,gcc++

1
yum install devtoolset-7-gcc devtoolset-7-gcc-c++

3、启动

1
sc enable devtoolset bash

需要注意的是scl命令启用只是临时的,退出shell或重启就会恢复原系统的gcc版本。如果要长期使用gcc 7,可以添加环境变量:

1
2
3
vim /etc/profile.d/gcc.sh

source /opt/rh/devtoolset-7/enable

更新环境变量:

1
source /etc/profile

4、查看GCC版本

1
2
3
4
5
6
7
# which gcc
/opt/rh/devtoolset-7/root/usr/bin/gcc
# gcc --version
gcc (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5)
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

GCC更新完毕。

编译安装openssl 11

在前面的安装测试中,不管是编译参数加–with-ssl-default-suites=openssl还是手动指定系统默认的openssl路径,安装完python3后,安装和更新python软件的时候,都会报错。

WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.

也手动改过/root/.config/pip/pip.conf配置文件:

[golbal]
index-url = https://mirrors.aliyun.com/pypi/simple/
将url的https请求改成http。这样是没有报错了,不过总感觉还是不解决实际问题。
于是,有了编译安装openssl的操作。

1
2
3
4
5
6
7
8
9
# 1、下载openssl 1.1.1
wget https://www.openssl.org/source/openssl-1.1.1l.tar.gz
# 2、解压
tar -zxvf openssl-1.1.1l.tar.gz
# 3、编译安装
cd openssl-1.1.1l
./config --prefix=/opt/openssl
make
make install

openssl的源码包可以点这里,openssl编译安装完成。

编译安装python3

目前的版本是python3.10.4。直接从官网下载源码。编译参数,安装如下:

1
2
3
4
5
6
7
8
9
10
11
# 1、下载python3源码
wget https://www.python.org/ftp/python/3.10.4/Python-3.10.4.tgz
# 2、解压
tar -zxvf Python-3.10.4.tgz
# 3、编译参数,指定安装目录,指定openssl目录
./configure --prefix=/usr/local/python3 --with-openssl=/opt/openssl --enable-optimizations
mark
mark install
# 4、创建软链接
ln -s /usr/local/python3/bin/python3 /usr/local/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/local/bin/pip3

安装完毕,可以看一下python3的版本信息,我们来验证一下openssl是否正常。

1
2
3
4
5
6
# python3 -V
Python 3.10.4
# pip3 -V
pip 22.0.4 from /usr/local/python3/lib/python3.10/site-packages/pip (python 3.10)
# python3 -c "import ssl;print(ssl.OPENSSL_VERSION)"
OpenSSL 1.1.1k FIPS 25 Mar 2021

好了,已经可以了。
openssl的版本已经正常出来了。

创建虚拟环境和包

Python应用程序通常会使用不在标准库内的软件包和模块。应用程序有时需要特定版本的库,因为应用程序可能需要修复特定的错误,或者可以使用库的过时版本的接口编写应用程序。

这意味着一个Python安装可能无法满足每个应用程序的要求。如果应用程序A需要特定模块的1.0版本但应用程序B需要2.0版本,则需求存在冲突,安装版本1.0或2.0将导致某一个应用程序无法运行。

这个问题的解决方案是创建一个 virtual environment,一个目录树,其中安装有特定Python版本,以及许多其他包。

然后,不同的应用将可以使用不同的虚拟环境。 要解决先前需求相冲突的例子,应用程序 A 可以拥有自己的 安装了 1.0 版本的虚拟环境,而应用程序 B 则拥有安装了 2.0 版本的另一个虚拟环境。 如果应用程序 B 要求将某个库升级到 3.0 版本,也不会影响应用程序 A 的环境。

创建虚拟环境

用于创建和管理虚拟环境的模块称为 venv。venv 通常会安装你可用的最新版本的 Python。如果您的系统上有多个版本的 Python,您可以通过运行 python3 或您想要的任何版本来选择特定的Python版本。

要创建虚拟环境,请确定要放置它的目录,并将 venv 模块作为脚本运行目录路径:

1
python3 -m venv tutorial-env

这将创建 tutorial-env 目录,如果它不存在的话,并在其中创建包含 Python 解释器副本和各种支持文件的目录。

虚拟环境的常用目录位置是 .venv。 这个名称通常会令该目录在你的终端中保持隐藏,从而避免需要对所在目录进行额外解释的一般名称。 它还能防止与某些工具所支持的 .env 环境变量定义文件发生冲突。

创建虚拟环境后,您可以激活它。

在Windows上,运行:

1
tutorial-env\Scripts\activate.bat

在Unix或MacOS上,运行:

1
source tutorial-env/bin/activate

(这个脚本是为bash shell编写的。如果你使用 csh 或 fish shell,你应该改用 activate.csh 或 activate.fish 脚本。)

激活虚拟环境将改变你所用终端的提示符,以显示你正在使用的虚拟环境,并修改环境以使 python 命令所运行的将是已安装的特定 Python 版本。 例如:

1
2
3
4
5
6
Python 3.10.4 (main, May  6 2022, 16:35:03) [GCC 7.3.1 20180303 (Red Hat 7.3.1-5)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/opt/rh/devtoolset-7/root/usr/lib64/python2.7/site-packages', '/opt/rh/devtoolset-7/root/usr/lib/python2.7/site-packages', '/usr/local/python3/lib/python310.zip', '/usr/local/python3/lib/python3.10', '/usr/local/python3/lib/python3.10/lib-dynload', '/opt/for_jumpserver/lib/python3.10/site-packages']
>>>

退出虚拟环境:

1
# deactivate

配置源

更换默认源至阿里云镜像。

临时使用

1
2
# 虚拟环境中操作
pip install -i https://mirrors.aliyun.com/pypi/simple 包名

长期使用

1
pip config set global.index-rul https://mirrors.aliyun.com/pypi/simple/

查看设置结果

1
2
# pip config list
golbal.index-url https://mirrors.aliyun.com/pypi/simple/

取消设置

1
pip config unset golbal.index-url 

也可以修改~.config/pip/pip.conf配置文件修改python源。


CentOS 7编译安装python3
https://ywmy.xyz/2022/05/06/CentOS-7编译安装python3/
作者
ian
发布于
2022年5月6日
许可协议