在Linux环境中下载安装python
1. 确认系统版本
[root@serverAliYun ~]# cat /etc/redhat-release CentOS Linux release 7.9.2009 (Core)
2. 安装依赖
这里openssl 部分是强相关的,如果不安装,后续 import ssl 会报错提示找不到 module. 代码中涉及到 https 的部分都无法运行。
[root@serverAliYun ~]# cat /etc/redhat-release CentOS Linux release 7.9.2009 (Core)
3. openssl 环境变量
export CFLAGS=$(pkg-config --cflags openssl11) export LDFLAGS=$(pkg-config --libs openssl11)
4. 下载python源码包
wget -P /test https://www.python.org/ftp/python/3.11.1/Python-3.11.1.tar.xz
5. 解压 && 移动
tar xvf Python-3.11.1.tar.xz mv Python-3.11.1 /usr/local/python3.11 cd /usr/local/python3.11/
6. 修改配置文件,vim Modules/Setup,取消以下部分的注释,让Python 在编译时就把 ssl 带上
第147行
_socket socketmodule.c
第215行 和 216行
_ssl _ssl.c $(OPENSSL_INCLUDES) $(OPENSSL_LDFLAGS) $(OPENSSL_LIBS)
_hashlib _hashopenssl.c $(OPENSSL_INCLUDES) $(OPENSSL_LDFLAGS) -lcrypto
7.?安装 && 编译
./configure --prefix=/usr/local/sbin/python3.11 make && make install
8. 等待安装完成
Installing collected packages: setuptools, pip WARNING: The scripts pip3 and pip3.11 are installed in '/usr/local/sbin/python3.11/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. Successfully installed pip-22.3.1 setuptools-65.5.0 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
9. 初步验证
[root@serverAliYun python3.11]# /usr/local/sbin/python3.11/bin/python3 -V Python 3.11.1 [root@serverAliYun python3.11]# /usr/local/sbin/python3.11/bin/python3 Python 3.11.1 (main, Jan 23 2023, 11:22:16) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import ssl >>> quit()
10. 移除旧软连接,创建新软连接
此时python 还是对应默认的 Python2.7,我们需要把他切换为 Python3 (还有个 Python-config 没有动,暂时保留)
# 查找 ll /usr/bin/ |grep python ll /usr/bin/ |grep pip # 删除 rm -rf /usr/bin/python rm -rf /usr/bin/pip # 重建 ln -sv /usr/local/sbin/python3.11/bin/python3 /usr/bin/python ln -s /usr/local/sbin/python3.11/bin/pip3 /usr/bin/pip
11. 确认效果
[root@serverAliYun ~]# python -V Python 3.11.1 [root@serverAliYun ~]# pip --version pip 22.3.1 from /usr/local/sbin/python3.11/lib/python3.11/site-packages/pip (python 3.11) [root@serverAliYun ~]#
12. 此时python默认已经是3.11,但CentOS yum 还是依赖Python2.7,需要修改 yum 相关配置文件,确保yum 依然能够正常运行
将/usr/bin/yum的顶部的: !/usr/bin/python 改成 !/usr/bin/python2.7 将/usr/libexec/urlgrabber-ext-down的顶部的: /usr/bin/python 改为 /usr/bin/python2.7 将/usr/bin/yum-config-manager的顶部的 #!/usr/bin/python 改为 #!/usr/bin/python2.7
mysql环境变量
export MYSQLCLIENT_CFLAGS="-I/usr/include/python3.9 -I/usr/include/mysql"
export MYSQLCLIENT_LDFLAGS="-L/usr/lib64/mysql -lmysqlclient"
y

浙公网安备 33010602011771号