centos7源码安装python3
2022-01-24 00:10
从源码安装python3
本文在centos7.6系统上实践
1. 安装编译所需依赖包
# yum -y groupinstall "Development tools"
# yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
# yum -y install libffi-deve wget -y
2. 下载并解压python3源码
此处下载最新的3.9.9版本,其他版本可以自行到https://www.python.org/ftp/python/
查看
/opt/python-3.9.9
可以根据习惯更换路径
# wget https://www.python.org/ftp/python/3.9.9/Python-3.9.9.tgz
# tar xzf Python-3.9.9.tgz
# cd Python-3.9.9
# mkdir /opt/python-3.9.9
3. 编译安装
# ./configure --prefix=/opt/python-3.9.9
# make && make install
4. 制作link文件
# ln -s /opt/python-3.9.9/bin/python3 /usr/local/bin/python3
# ln -s /opt/python-3.9.9/bin/pip3 /usr/local/bin/pip3
5. 查看安装结果
[root@test-centos-9 ~]# python3 -m site
sys.path = [
'/root',
'/opt/python-3.9.9/lib/python39.zip',
'/opt/python-3.9.9/lib/python3.9',
'/opt/python-3.9.9/lib/python3.9/lib-dynload',
'/opt/python-3.9.9/lib/python3.9/site-packages',
]
USER_BASE: '/root/.local' (doesn't exist)
USER_SITE: '/root/.local/lib/python3.9/site-packages' (doesn't exist)
ENABLE_USER_SITE: True
6. 注意事项
python3安装后,不要卸载原有python2的任何包,因为python3和python2有很多不兼容的地方
操作系统有很多工具都依赖原有的python2,直接替换会导致包括在yum
在内的很多包不可用
可以用以下方式简单粗暴且不精准的方式查看到系统中依赖原有python2的程序或库
[root@test-centos-9 ~]# grep "\#\!/usr/bin/python" /* -rnI
/bin/pydoc:1:#!/usr/bin/python2.7
/bin/msghack:1:#!/usr/bin/python
/bin/urlgrabber:1:#!/usr/bin/python -t
/bin/yum:1:#!/usr/bin/python
/bin/firewall-cmd:1:#!/usr/bin/python -Es
/bin/systemd-sysv-convert:1:#!/usr/bin/python
/bin/firewall-offline-cmd:1:#!/usr/bin/python -Es
/lib/python2.7/site-packages/slip/dbus/introspection.py:1:#!/usr/bin/python
... # 太多了,省略了