背景
由于公司最近云机器迁移,将关停部分机器,并将其一些脚本转移到既有机器继续运行,而恰好需要将一份python3脚本迁移到既有CentOS6上。
步骤
编译源码
1.可用包依赖
# CentOS6
yum install gcc glibc make perl patch zlib-devel bzip2-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel libuuid-devel -y
# CentOS7(如只想安装到3.9,可以再install openssl-devel)
yum install gcc glibc make perl patch zlib-devel bzip2-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel libuuid-devel sqlite-devel -y
2.安装源码依赖
说明
Python3.7依赖
- 最低sqlite3.3.9
- openssl1.0.2或1.1.x(不支持3以上)
Python3.8-3.9依赖
- 最低sqlite3.7.2
- openssl1.0.2或1.1.x(不支持3以上)
Python3.10依赖
- 最低sqlite3.7.15
- openssl1.1.1(不支持3以上)
Python3.11依赖
- 最低sqlite3.7.15
- openssl1.1.1(不支持3以上)
- 需要c11支持
CentOS6自带
- sqlite3.6.20
- openssl1.0.1
- gcc4.4.7(从4.7开始才支持C11)
CentOS7自带
- sqlite3.7.17
- openssl1.0.2
- gcc4.8.5(支持C11)
安装
openssl
由于都可以用openssl1.1.1版本,因此直接用此版本编译,当前最新版为1.1.1s
curl "https://www.openssl.org/source/openssl-1.1.1s.tar.gz" -o openssl-1.1.1s.tar.gz
tar -zxvf openssl-1.1.1s.tar.gz
cd openssl-1.1.1s/
./config no-asm no-shared --prefix=/usr/local/openssl
make
make install
no-asm: 不使用汇编语言加速编译,因为汇编还不支持arm平台。
no-shared: 不生成动态连接库。
参照:
sqlite3
由于CentOS6是2020年12月2日结束支持,但yum源停留在2018年,因此使用2018年最后编译的版本3.26.0
curl "https://www.sqlite.org/2018/sqlite-autoconf-3260000.tar.gz" -o sqlite-autoconf-3260000.tar.gz
tar -zxvf sqlite-autoconf-3260000.tar.gz
cd sqlite-autoconf-3260000/
./configure --prefix=/usr/local/
make
make install
# 安装完毕后,要让Python识别到,因此要配置下ld.so.conf.d
cat>/etc/ld.so.conf.d/local.conf<<EOF
/usr/local/lib
EOF
# 让配置生效
ldconfig
CentOS7可忽略这个编译,因为已满足最低安装限制。
参照:
gcc
因为Python-3.11开始使用C11标准编译,而从4.7开始才支持C11,从4.8开始全面支持C11。因此CentOS6需要升级gcc版本来支持C11编译。
curl "https://people.centos.org/tru/devtools-2/devtools-2.repo" -o "/etc/yum.repos.d/devtoolset-2.repo"
yum -y install devtoolset-2-gcc devtoolset-2-gcc-c++ devtoolset-2-binutils
scl enable devtoolset-2 bash
参照:
3.安装Python3
最新版
- Python-3.11.0
- Python-3.10.8
- Python-3.9.15
- Python-3.8.15
- Python-3.7.15
下载
以3.7.15为例,其他版本只要将3.7.15替换为相应版本即可。
curl "https://www.python.org/ftp/python/3.7.15/Python-3.7.15.tgz" -o Python-3.7.15.tgz
tar -zxvf Python-3.7.15.tgz
cd Python-3.7.15
安装
1.1.CentOS6
如果安装3.7版本,那么无需编译sqlite,只需再下载依赖
yum install sqlite-devel -y
之后步骤都一样
./configure --with-openssl=/usr/local/openssl --prefix=/usr/local/python3
make
make install
1.2.CentOS7
无需编译sqlite,如果安装3.9或以下版本,也可不用编译openssl
./configure --prefix=/usr/local/python3
make
make install
安装3.10或以上版本
./configure --with-openssl=/usr/local/openssl --prefix=/usr/local/python3
make
make install
2.性能及优化
安装以上的编译,只是保证能用,并没有做性能的优化处理,可加上--enable-optimizations,此标志启用配置文件引导的优化(PGO)和链接时间优化(LTO)。
--enable-optimizations 都做了什么?编译速度是真的慢。
此标志启用配置文件引导的优化(PGO)和链接时间优化(LTO).
这都是昂贵的优化,它们会减慢构建过程,但会显着提高速度(我记得阅读的内容大约有10-20%).
这些确切功能的讨论超出了我的理解,可能对于一个问题来说范围太广.无论哪种方式,您都可以从有关GCC的文档中了解一些有关LTO的信息.实现并通过阅读其Wiki页面开始使用PGO.
此外,请参阅在添加了这些功能的Python Bug Tracker上打开的相关问题:
问题24915 :配置文件引导的优化改进(更好的培训,llvm支持等)(已添加PGO.)
问题25702 :对GCC和CLANG的链接时间优化支持(已添加LTO.)
问题26359 : CPython构建用于实现即席即用性能的选项(将--enable-optimizations标志添加到配置脚本中,以启用上述优化.)
@Shuo在评论中指出并在问题28032 中指出,LTO是"t始终使用--enable-optimizations标志启用 .某些平台(取决于gcc 的受支持版本)会在配置脚本中将其禁用.
此标志的未来版本可能始终会启用它,因此在这里讨论它们都是很安全的.
参照:
3.软连接
ln -s /usr/local/python3/bin/python3 /usr/local/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/local/bin/pip3
4.验证
python3 -V
pip3 -V
yum源安装
epel
1.安装
yum install epel-release
yum install python34 python34-pip
2.验证
python3 -V
pip3 -V
scl
1.安装
yum install centos-release-scl centos-release-scl-rh
# 由于不再维护,需要换源
sed -e "s|^mirrorlist=|#mirrorlist=|g" -e "s|^# baseurl=http://mirror.centos.org|baseurl=https://mirrors.aliyun.com/centos-vault|g" -i.bak /etc/yum.repos.d/CentOS-SCLo-scl.repo
sed -e "s|^mirrorlist=|#mirrorlist=|g" -e "s|^#baseurl=http://mirror.centos.org|baseurl=https://mirrors.aliyun.com/centos-vault|g" -i.bak /etc/yum.repos.d/CentOS-SCLo-scl-rh.repo
yum makecache
yum install rh-python36
2.验证
# 方式一
scl enable rh-python36 bash
python -V
pip -V
exit
# 方式二
# 软连接
ln -s /opt/rh/rh-python36/root/usr/bin/python /usr/local/bin/python3
ln -s /opt/rh/rh-python36/root/usr/bin/pip /usr/local/bin/pip3
# 验证
python3 -V
pip3 -V
扩展
4.安装华为云模块
# pip3 install huaweicloudsdkcore huaweicloudsdkecs
无法加载ssl模块
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Collecting huaweicloudsdkcore
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/huaweicloudsdkcore/
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/huaweicloudsdkcore/
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/huaweicloudsdkcore/
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/huaweicloudsdkcore/
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/huaweicloudsdkcore/
Could not fetch URL https://pypi.org/simple/huaweicloudsdkcore/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/huaweicloudsdkcore/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
ERROR: Could not find a version that satisfies the requirement huaweicloudsdkcore (from versions: none)
ERROR: No matching distribution found for huaweicloudsdkcore
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
这是源码安装时SSL模块没有加载到,导致无法访问https的地址导致的,解决如下:
./configure --prefix=/usr/local/python3 --with-openssl=/usr/local/openssl --with-openssl-rpath=auto
在非root用户下,由于openssl包和源码都有了,运行时会依赖混乱,因此要加上--with-openssl-rpath=auto
出现问题:ERROR: Could not find a version that satisfies the requirement XXX解决方法
直接选用pip源并且信任它的来源就可以解决这种问题。
pip install 库包名 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
这里将pip源换成清华源、阿里源等都适用。
更换其他镜像源
清华镜像源: https://pypi.tuna.tsinghua.edu.cn/simple
中国科学技术大学 : https://pypi.mirrors.ustc.edu.cn/simple
豆瓣:http://pypi.douban.com/simple/
阿里: http://mirrors.aliyun.com/simple/