在 CentOS 7 下编译安装 Python 3

关键字

CentOS, Python, CentOS 7, Python 3

背景概述

Python 近年变得炙手可热,家喻户晓,并非一朝一夕。其原因一定程度上,可以在 python 中运行:import this 得到答案。

目前,Python 有两个版本并行开发,Python 2 和 Python 3。可预见的是,在随后 5 年内,Python 2 和 Python 3 的完全合并都不是一件容易的事,如果你的程序需要兼容两个版本,你可以使用 six 等兼容库来编写一套代码兼容两个版本。

想了解背后分叉的详细原因,可参见文章《Python 软件名片》、《Python 工程实践》等。

在 CentOS 7 中,自带的是 Python 2.7 版本,且系统中不少命令与其关联,比如 yum 包管理工具。因此当我们需要使用 Python 3 时,我们就不能直接将系统自带的 Python 2 升级,而是需要自行编译安装 Python 3 与 Python 2 并存。

本文旨在帮助读者在 CentOS 7 下顺利安装 Python 3 最新版,本安装过程应已经过笔者反复测试,以确保成功。

如果按此文安装时遇到任何问题,请留言或通过【桃花岛】官方联系我们。

软件依赖

  • CentOS 7
  • Python 3.7.4 源码
  • GCC 编译工具
  • wget

具体步骤

yum 安装

CentOS 7 仓库自带 Python 2,截止本文发布,版本是 Python 2.7.5。

[root@carbon ~]# python
Python 2.7.5 (default, Jun 20 2019, 20:27:34) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> copyright
Copyright (c) 2001-2013 Python Software Foundation.
All Rights Reserved.

Copyright (c) 2000 BeOpen.com.
All Rights Reserved.

Copyright (c) 1995-2001 Corporation for National Research Initiatives.
All Rights Reserved.

Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.
All Rights Reserved.
>>> license
See http://www.python.org/2.7/license.html
>>> 

请不要删除系统自带的 Python 2,而只用 yum update 命令升级它。但 yum update 不会自动将 Python 2 升级为 Python 3。

编译安装 Python 3 ,最新版

首先,安装编译工具和依赖包:

sudo yum groupinstall "development tools"
sudo yum install bzip2-devel gdbm-devel libffi-devel libuuid-devel ncurses-devel openssl-devel readline-devel sqlite-devel tk-devel wget xz-devel zlib-devel

然后,访问 Python 官方下载网址,找到最新版源代码地址,如果 wget 无法下载,可以使用迅雷下载后上传服务器目录下,再继续运行:

cd /usr/local/src/
wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz
tar xzf Python-3.7.4.tgz
cd Python-3.7.4
./configure --enable-shared

如果你想编译一个所有稳定的优化打开的版本,比如 PGO,等,可以执行 ./configure –enable-optimizations

运行 make:

make

命令返回:

[root@carbon Python-3.7.4]# make
...
LD_LIBRARY_PATH=/usr/local/src/Python-3.7.4 CC='gcc -pthread' LDSHARED='gcc -pthread -shared    ' OPT='-DNDEBUG -g -fwrapv -O3 -Wall'   _TCLTK_INCLUDES='' _TCLTK_LIBS=''       ./python -E ./setup.py  build
running build
running build_ext

The following modules found by detect_modules() in setup.py, have been
built by the Makefile instead, as configured by the Setup files:
_abc                  atexit                pwd                
time                                                           

running build_scripts
copying and adjusting /usr/local/src/Python-3.7.4/Tools/scripts/pydoc3 -> build/scripts-3.7
copying and adjusting /usr/local/src/Python-3.7.4/Tools/scripts/idle3 -> build/scripts-3.7
copying and adjusting /usr/local/src/Python-3.7.4/Tools/scripts/2to3 -> build/scripts-3.7
copying and adjusting /usr/local/src/Python-3.7.4/Tools/scripts/pyvenv -> build/scripts-3.7
changing mode of build/scripts-3.7/pydoc3 from 644 to 755
changing mode of build/scripts-3.7/idle3 from 644 to 755
changing mode of build/scripts-3.7/2to3 from 644 to 755
changing mode of build/scripts-3.7/pyvenv from 644 to 755
renaming build/scripts-3.7/pydoc3 to build/scripts-3.7/pydoc3.7
renaming build/scripts-3.7/idle3 to build/scripts-3.7/idle3.7
renaming build/scripts-3.7/2to3 to build/scripts-3.7/2to3-3.7
renaming build/scripts-3.7/pyvenv to build/scripts-3.7/pyvenv-3.7

继续运行:

make install

命令返回:

...
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-19.0.3 setuptools-40.8.0

将 Python 3 依赖的库放入系统

cp /usr/local/src/Python-3.7.4/libpython3.7m.so.1.0 /usr/lib64

安装验证

运行 Python 3 命令行

[root@carbon ~]# python3
Python 3.7.4 (default, Aug 20 2019, 17:14:36) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

此时,可运行 which 命令看看 python3 的路径

[root@carbon ~]# which python3
/usr/local/bin/python3
[root@carbon ~]# cd /usr/local/bin
[root@carbon bin]# ls python*
python3  python3.7  python3.7-config  python3.7m  python3.7m-config  python3-config

问题汇总

问题:运行 python3 提示 libpython3.7m.so.1.0 不存在

[root@carbon ~]# python3
python3: error while loading shared libraries: libpython3.7m.so.1.0: cannot open shared object file: No such file or directory

解决:将 libpython3.7m.so.1.0 放到系统库目录下

cp /usr/local/src/Python-3.7.4/libpython3.7m.so.1.0 /usr/lib64

问题:wget 下载 Python 3 源码不成功:

[root@carbon src]# wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz
--2019-08-20 16:39:00--  https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz
Resolving www.python.org (www.python.org)... failed: Name or service not known.
wget: unable to resolve host address ‘www.python.org’
[root@carbon src]# wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz
--2019-08-20 16:39:36--  https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz
Resolving www.python.org (www.python.org)... 151.101.108.223, 2a04:4e42:1a::223
Connecting to www.python.org (www.python.org)|151.101.108.223|:443... failed: Connection timed out.
Connecting to www.python.org (www.python.org)|2a04:4e42:1a::223|:443... failed: Network is unreachable.

解决:改用迅雷等下载工具下载,并上传至服务器目录下,继续解压编译。

问题:Failed to build these modules: _ctypes

Python build finished successfully!
The necessary bits to build these optional modules were not found:
_bz2                  _dbm                  _gdbm              
_sqlite3              _tkinter              _uuid              
readline                                                       
To find the necessary bits, look in setup.py in detect_modules() for the module's name.


The following modules found by detect_modules() in setup.py, have been
built by the Makefile instead, as configured by the Setup files:
_abc                  atexit                pwd                
time                                                           


Failed to build these modules:
_ctypes

解决:配置语句带上参数 ./configure –enable-shared

./configure --enable-shared

问题:Failed to build these modules: _uuid

Failed to build these modules:
_ctypes

解决:配置语句带上参数 ./configure –enable-shared

./configure --enable-shared
标签:

发表评论

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