python3源码方式安装
系统默认的python3版本过低,得自己安装高版本python3。步骤主要为:1、删除旧版本。2、安装依赖。3、下载源码。4、配置。5、编译。可以使用root操作。参考链接:CentOS7安装python3超详细教程、centos7安装python3.11完整教程(完整有效)
1、删除旧版pyhton3,执行:rpm -qa|grep python3.7|xargs rpm -ev --allmatches --nodeps (谨慎执行,先看下会删除哪些依赖,一不小可能删掉其他组件的依赖,比如yum)
2、安装依赖,执行:yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make libffi-devel
ps:有些依赖是跟后续组件需要的:
1、libffi-devel缺失导致"_ctypes"错; 2、gcc默认版本可能过低,需要手工编译安装:https://blog.csdn.net/weixin_38642130/article/details/86412609/ 3、还有几个建议也装上,后续用torch或milvus需要:xz-devel、libmpc-devel、perl-ExtUtils-CBuilder、perl-ExtUtils-MakeMaker、python-backports-lzma
3、下载新版本,网址:https://www.python.org/ftp/python/
4、配置,解压源码(tar zxvf 源码包),执行(编译时带上ssl模块):./configure --prefix=/usr/local --with-openssl=/usr/local/ --with-openssl-rpath=auto
ps:需要编译ssl模块,否则后面pip安装组件会报错。ssl模块的问题可以参看:https://www.cnblogs.com/lemon-le/p/13419429.html?ivk_sa=1024320u 关注下面的输出:
checking for openssl/ssl.h in /usr/local/... yes checking whether compiling and linking against OpenSSL works... yes checking for --with-openssl-rpath... auto checking whether OpenSSL provides required APIs... yes checking for --with-ssl-default-suites... python
5、编译(make -j 4),安装(make install)。如果安装路径设置为/usr/local,则下面的步骤可以不做了,/usr/local/bin本来就在path变量中。
6、建立软链接:
ln -s /usr/local/bin/python3.12 /usr/bin/python3 ln -s /usr/local/bin/pip3 /usr/bin/pip3
ps:需要关注pip3的内容,import的方式可能要用from pip._internal.main import main,而不是只有from pip._internal.cli.main import main:
#!/usr/bin/python3 # -*- coding: utf-8 -*- import re import sys try: from pip._internal.cli.main import main except ImportError: try: from pip._internal.main import main except ImportError: try: # If the user has downgraded pip, the above import will fail. # Let's try older methods of invoking it: # pip 19 uses this from pip._internal import main except ImportError: # older pip versions use this from pip import main if __name__ == '__main__': sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) sys.exit(main())
7、配置环境:修改/etc/profile文件:
export PYTHON_HOME=/usr/local
export PATH=$PYTHON_HOME/bin:$PATH