系统默认的python是2.7.5版本

# python --version
Python 2.7.5 

升级有多种方式, 我们这里用源代码编译的方式

  1. 先安装依赖组件, 主要是gcc编译器

bzip2-devel-1.0.6-13.el7.x86_64.rpm
cpp-4.8.5-44.el7.x86_64.rpm
dejavu-fonts-common-2.33-6.el7.noarch.rpm
dejavu-sans-fonts-2.33-6.el7.noarch.rpm
deltarpm-3.6-3.el7.x86_64.rpm
expat-devel-2.1.0-12.el7.x86_64.rpm
fontconfig-2.13.0-4.3.el7.x86_64.rpm
fontconfig-devel-2.13.0-4.3.el7.x86_64.rpm
fontpackages-filesystem-1.44-8.el7.noarch.rpm
freetype-2.8-14.el7_9.1.x86_64.rpm
freetype-devel-2.8-14.el7_9.1.x86_64.rpm
gcc-4.8.5-44.el7.x86_64.rpm
glibc-2.17-322.el7_9.x86_64.rpm
glibc-common-2.17-322.el7_9.x86_64.rpm
glibc-devel-2.17-322.el7_9.x86_64.rpm
glibc-headers-2.17-322.el7_9.x86_64.rpm
gpm-libs-1.20.7-6.el7.x86_64.rpm
htop-2.2.0-3.el7.x86_64.rpm
initscripts-9.49.53-1.el7_9.1.x86_64.rpm
kde-filesystem-4-47.el7.x86_64.rpm
kde-l10n-4.10.5-2.el7.noarch.rpm
kde-l10n-Chinese-4.10.5-2.el7.noarch.rpm
kernel-headers-3.10.0-1160.15.2.el7.x86_64.rpm
keyutils-libs-devel-1.5.8-3.el7.x86_64.rpm
krb5-devel-1.15.1-50.el7.x86_64.rpm
libblkid-2.23.2-65.el7_9.1.x86_64.rpm
libcom_err-devel-1.42.9-19.el7.x86_64.rpm
libffi-devel-3.0.13-19.el7.x86_64.rpm
libkadm5-1.15.1-50.el7.x86_64.rpm
libmount-2.23.2-65.el7_9.1.x86_64.rpm
libmpc-1.0.1-3.el7.x86_64.rpm
libpng-devel-1.5.13-8.el7.x86_64.rpm
libselinux-devel-2.5-15.el7.x86_64.rpm
libsepol-devel-2.5-10.el7.x86_64.rpm
libsmartcols-2.23.2-65.el7_9.1.x86_64.rpm
libuuid-2.23.2-65.el7_9.1.x86_64.rpm
libuuid-devel-2.23.2-65.el7_9.1.x86_64.rpm
libverto-devel-0.2.5-4.el7.x86_64.rpm
libX11-1.6.7-3.el7_9.x86_64.rpm
libX11-common-1.6.7-3.el7_9.noarch.rpm
libX11-devel-1.6.7-3.el7_9.x86_64.rpm
libXau-1.0.8-2.1.el7.x86_64.rpm
libXau-devel-1.0.8-2.1.el7.x86_64.rpm
libxcb-1.13-1.el7.x86_64.rpm
libxcb-devel-1.13-1.el7.x86_64.rpm
libXft-2.3.2-2.el7.x86_64.rpm
libXft-devel-2.3.2-2.el7.x86_64.rpm
libXrender-0.9.10-1.el7.x86_64.rpm
libXrender-devel-0.9.10-1.el7.x86_64.rpm
lrzsz-0.12.20-36.el7.x86_64.rpm
mpfr-3.1.1-4.el7.x86_64.rpm
ncurses-devel-5.9-14.20130511.el7_4.x86_64.rpm
openssl-1.0.2k-21.el7_9.x86_64.rpm
openssl-devel-1.0.2k-21.el7_9.x86_64.rpm
openssl-libs-1.0.2k-21.el7_9.x86_64.rpm
pcre-devel-8.32-17.el7.x86_64.rpm
perl-5.16.3-299.el7_9.x86_64.rpm
perl-Carp-1.26-244.el7.noarch.rpm
perl-constant-1.27-2.el7.noarch.rpm
perl-Encode-2.51-7.el7.x86_64.rpm
perl-Exporter-5.68-3.el7.noarch.rpm
perl-File-Path-2.09-2.el7.noarch.rpm
perl-File-Temp-0.23.01-3.el7.noarch.rpm
perl-Filter-1.49-3.el7.x86_64.rpm
perl-Getopt-Long-2.40-3.el7.noarch.rpm
perl-HTTP-Tiny-0.033-3.el7.noarch.rpm
perl-libs-5.16.3-299.el7_9.x86_64.rpm
perl-macros-5.16.3-299.el7_9.x86_64.rpm
perl-parent-0.225-244.el7.noarch.rpm
perl-PathTools-3.40-5.el7.x86_64.rpm
perl-Pod-Escapes-1.04-299.el7_9.noarch.rpm
perl-podlators-2.5.1-3.el7.noarch.rpm
perl-Pod-Perldoc-3.20-4.el7.noarch.rpm
perl-Pod-Simple-3.28-4.el7.noarch.rpm
perl-Pod-Usage-1.63-3.el7.noarch.rpm
perl-Scalar-List-Utils-1.27-248.el7.x86_64.rpm
perl-Socket-2.010-5.el7.x86_64.rpm
perl-Storable-2.45-3.el7.x86_64.rpm
perl-Text-ParseWords-3.29-4.el7.noarch.rpm
perl-threads-1.87-4.el7.x86_64.rpm
perl-threads-shared-1.43-6.el7.x86_64.rpm
perl-Time-HiRes-1.9725-3.el7.x86_64.rpm
perl-Time-Local-1.2300-2.el7.noarch.rpm
readline-devel-6.2-11.el7.x86_64.rpm
sqlite-devel-3.7.17-8.el7_7.1.x86_64.rpm
sshpass-1.06-2.el7.x86_64.rpm
tcl-8.5.13-8.el7.x86_64.rpm
tcl-devel-8.5.13-8.el7.x86_64.rpm
telnet-server-0.17-66.el7.x86_64.rpm
tk-8.5.13-6.el7.x86_64.rpm
tk-devel-8.5.13-6.el7.x86_64.rpm
util-linux-2.23.2-65.el7_9.1.x86_64.rpm
vim-common-7.4.629-8.el7_9.x86_64.rpm
vim-enhanced-7.4.629-8.el7_9.x86_64.rpm
vim-filesystem-7.4.629-8.el7_9.x86_64.rpm
wget-1.14-18.el7_6.1.x86_64.rpm
xorg-x11-proto-devel-2018.4-1.el7.noarch.rpm
zlib-1.2.7-19.el7_9.x86_64.rpm
zlib-devel-1.2.7-19.el7_9.x86_64.rpm

2.下载源码, 解压

curl -o python-3.7.9.source.tar https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tgz
tar zxf  python-3.7.9.source.tar 
cd Python-3.7.9

3. 编译

set -ex \
&& tar -zxvf Python-3.7.9.tgz \
&& cd Python-3.7.9 \
&& ./configure -q -enable-shared prefix=/usr/local/python3 \
&& make -s \
&& make install -s \
&& make clean -s \
&& rm -rf /Python-3.7.9* \
&& echo "/usr/local/python3/lib" > /etc/ld.so.conf.d/python3.conf \
&& ldconfig
#&& yum install -y epel-release \
#&& yum install -y python-pip
# 备份旧版本python,设置默认为python3

ln -s python2.7 /usr/bin/python2

mv -f /usr/bin/pip /usr/bin/pip2

set -ex \
&& rm -f /usr/bin/python \
&& ln -s /usr/local/python3/bin/python3.7 /usr/bin/python3 \
&& ln -s /usr/local/python3/bin/python3.7 /usr/bin/python \
&& sed -i "s#/usr/bin/python#/usr/bin/python2#" /etc/profile \
&& sed -i "s#/usr/bin/python22#/usr/bin/python2#" /etc/profile

set -ex \
&& ln -s /usr/local/python3/bin/pip3 /usr/bin/pip \
&& ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
# 修复因修改python版本导致yum失效问题
set -ex \
&& sed -i "s#/usr/bin/python#/usr/bin/python2#" /usr/bin/yum \
&& sed -i "s#/usr/bin/python22#/usr/bin/python2#" /usr/bin/yum \
&& sed -i "s#/usr/bin/python#/usr/bin/python2#" /usr/libexec/urlgrabber-ext-down \
&& sed -i "s#/usr/bin/python22#/usr/bin/python2#" /usr/libexec/urlgrabber-ext-down
# && yum install -y deltarpm
# 基础环境配置
set -ex \
&& rm -rf /etc/localtime \
&& ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# && yum install -y vim \
# && yum -y install cronie
# 支持中文
# yum install kde-l10n-Chinese -y
localedef -c -f UTF-8 -i zh_CN zh_CN.utf8
# 更新pip版本
pip install --upgrade pip

4. 修复yum

如果/usr/bin/下还有其他/usr/bin/yum*文件, 则同样需要执行:

sed -i "s#/usr/bin/python#/usr/bin/python2#" /usr/bin/yumXXX
sed -i "s#/usr/bin/python22#/usr/bin/python2#" /usr/bin/yumXXX

5. FAQ

安装报错 [Objects/unicodeobject.o] Error gcc: internal compiler error: Killed (program cc1)

修改Makefile 把‘-DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes’中的‘O3’改为‘O2’或‘O0’不执行优化即可

# 还有一种可能是内存不足, 这时候需要用增加swap来处理, 这里是512M
sudo mkdir -p /var/cache/swap/
sudo dd if=/dev/zero of=/var/cache/swap/swap0 bs=1M count=512
sudo chmod 0600 /var/cache/swap/swap0
sudo mkswap /var/cache/swap/swap0
sudo swapon /var/cache/swap/swap0
参考资料:
http://stackoverflow.com/questions/19761226/how-to-compile-ruby-with-rvm-on-a-low-memory-system
https://github.com/pydata/pandas/issues/1880#issuecomment-9920484 

安装后, 报python3.x.so.1.0找不到问题

# 先执行ldd, 看看是不是少了so文件 
ldd /usr/local/python3.7.9/bin/python3
# 把Not Found的so拷贝到系统lib下
cp /usr/local/python3.7.9/lib/XXX /lib64/

发表评论

邮箱地址不会被公开。 必填项已用*标注