一、查看CentOS版本、系统默认gcc版本、Python版本和pip版本:
# cat /etc/redhat-release
# gcc --version
# python -V
# pip -V
二、部署Python-3.10.13:
1、下载Python-3.10.13.tar.xz,Python官网:https://www.python.org/
2、安装编译依赖软件包及包组:
# 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 man
3、编译安装Python-3.10.13:
# tar -xf Python-3.10.13.tar.xz -C /usr/src
# cd /usr/src/Python-3.10.13
# ./configure --prefix=/usr/local/python-3.10.13 --enable-shared --enable-profiling --disable-ipv6 --with-pymalloc --with-doc-strings --enable-loadable-sqlite-extensions | tee /tmp/python-3.10.13.out
说明:./configure时不要加--enable-optimizations选项,会报错
# less /tmp/python-3.10.13.out
# make -j 8
# make install
4、更改CentOS 7.9默认Python版本为3.10.13:
# which python
# mv /usr/bin/python /usr/bin/python-2.7.5
# ln -s /usr/local/python-3.10.13/bin/python3 /usr/bin/python
# ln -s /usr/local/python-3.10.13/bin/pip3 /usr/bin/pip
5、添加至PATH环境变量:
# vim /etc/profile.d/python-3.10.13.sh
export PATH=/usr/local/python-3.10.13/bin:$PATH
# . /etc/profile.d/python-3.10.13.sh
# echo $PATH
6、配置头文件:
# ln -s /usr/local/python-3.10.13/include /usr/include/python-3.10.13
7、配置库文件:
# echo "/usr/local/python-3.10.13/lib" > /etc/ld.so.conf.d/python-3.10.13.conf
# cat /etc/ld.so.conf.d/python-3.10.13.conf
# ldconfig
8、查看部署后的Python和pip版本:
# python -V
# python3 -V
# pip -V
9、写代码测试:
# vim /tmp/test.py
#!/usr/bin/python
import os
# 获取操作系统类型
os_type = os.uname().sysname
print("操作系统类型:",os_type)
# 获取操作系统版本
os_version = os.uname().release
print("操作系统版本:",os_version)
# 获取主机名
hostname = os.uname().nodename
print("主机名:",hostname)
# 获取CPU信息
cpu_info = os.popen('cat /proc/cpuinfo | grep "model name"').read().strip()
print("CPU信息:")
print(cpu_info)
# 获取内存信息
mem_info = os.popen('cat /proc/meminfo | grep "MemTotal"').read().strip()
print("内存信息:")
print(mem_info)
# 获取磁盘使用情况
disk_usage = os.popen('df -h').read().strip()
print("磁盘使用情况:")
print(disk_usage)
# 获取/tmp目录下的文件信息
print("/tmp目录下的文件信息:")
os.system('ls -lh /tmp')
# python /tmp/test.py
三、将yum中的Python版本修改为系统原来的2.7.5版本:
升级Python后会导致yum无法使用:
# yum -y install htop
# vim /usr/bin/yum,将第一行的“#!/usr/bin/python”修改为“#!/usr/bin/python-2.7.5”
再次执行安装命令,提示:
# vim /usr/libexec/urlgrabber-ext-down,将第一行的“#! /usr/bin/python”修改为“#! /usr/bin/python-2.7.5”