编程,作为生物信息学的一个基础性技能,是任何一个生信工程师都无法绕开话题。也许有些人还在纠结 Perl 和 Python 到底应该学习哪一个,但作为目前最火最流行的编程语言 Python 还是非常值得尝试的。它不但可以进行文本处理,在统计、网站、游戏、爬虫、数据可视化等方面也有非常强大的应用,比起曾经的 Perl 真的强大和全面很多,且比 Perl 更容易入手。不管从长远发展,还是短期需要,学会 Python,看懂 Perl (或者先学 Python,后学 Perl) 应该是每一个生信工程必备的基础技能之一。

工欲善其事,必先利其器。关于 Python 安装教程在网上一搜一大把,但总感觉不够全面,尤其对于中间出现的一些问题的解决方法不尽如人意。鉴于此,本文基于 CentOS/Ubuntu Linux 对 Python 的源码编译安装进行了一下简单的总结,记录如下。

  • Update 2021-12-21:基于本文档在 Ubuntu 20.04 LTS 安装 Python-3.10.1 成功!
  • Update 2021-09-15:参考此文档在 Red Hat Enterprise 6.5 安装 Python-3.9.5 成功!

1. CentOS/RedHat

以下的内容中,我们以安装 Python-3.7.3 为例进行说明。

1.1 安装环境

Red Hat 6.5 + GCC 4.4.7(GCC-4.8.5/5.3.1)。GCC 高级版本手动/yum 安装参考以下文章。

  1. $ lsb_release -a
  2. LSB Version: :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
  3. Distributor ID: RedHatEnterpriseServer
  4. Description: Red Hat Enterprise Linux Server release 6.5 (Santiago)
  5. Release: 6.5
  6. Codename: Santiago
  7. $ gcc --version
  8. gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-4)
  9. Copyright (C) 2010 Free Software Foundation, Inc.
  10. This is free software; see the source for copying conditions. There is NO
  11. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

1.2 解决依赖

如果您拥有 root 权限,请执以下依赖安装:

  1. yum install zlib
  2. yum install zlib-devel
  3. yum install openssl
  4. yum install openssl-devel
  5. yum install libffi
  6. yum install libffi-devel
  7. yum install readline readline-devel sqlite sqlite-devel tk-devel
缺少库名称 安装命令
_uuid yum install libuuid-devel
readline yum install readline-devel
_tkinter yum install tk-devel
_ffi yum install libffi-devel
_curses yum install ncurses-libs
_sqlite yum install sqlite-devel
_bz2 yum isntall bzip2-devel
_ssl yum install openssl-devel
_gdbm yum install gdbm-devel
_dbi yum install libdbi-devel
_zlib yum install zlib-devel
lzma yum install xz-devel
yum install python-backports-lzma

如果您没有 root 权限,可以参考《手把手教你在 Linux 源码安装最新版本的 R》一文,手动一个个去解决以上的依赖。

1.2.1 _sqlite3 依赖

执行 make 过程中提示 _sqlite3 not found,如下:

  1. $ make
  2. ......
  3. Python build finished successfully!
  4. The necessary bits to build these optional modules were not found:
  5. _sqlite3 _ssl
  6. To find the necessary bits, look in setup.py in detect_modules() for the module's name.
  7. The following modules found by detect_modules() in setup.py, have been
  8. built by the Makefile instead, as configured by the Setup files:
  9. _abc atexit pwd
  10. time

如果执行 rpm -qa|grep sqlite 看到 sqlite 和 sqlite-devel 都已经安装(libsqlite3.so 默认保存在 /usr/lib64 下; sqlite3.h 默认保存在 /usr/include 下)。

  1. $ sqlite3 -version
  2. 3.6.20
  3. $ ll /usr/lib64/libsqlite3.so
  4. lrwxrwxrwx 1 root root 19 Apr 23 2015 /usr/lib64/libsqlite3.so -> libsqlite3.so.0.8.6
  5. $ ll /usr/include/sqlite3.h
  6. -rw-r--r-- 1 root root 263K Nov 25 2009 /usr/include/sqlite3.h

但是,执行 make 依然出现以上报错,参考下面的方法《python build from source: cannot build optional module sqlite3 - Stack Overflow》。

  1. 手动安装 sqlite3。

    1. $ wget https://www.sqlite.org/2021/sqlite-autoconf-3360000.tar.gz --no-check-certificate
    2. $ tar zvxf sqlite-autoconf-3360000.tar.gz
    3. $ cd sqlite-autoconf-3360000
    4. $ ./configure --prefix=/Bioinfo/Pipeline/SoftWare/sqlite-3.36.0
    5. $ make
    6. $ make install
  2. 找到 sqlite3.h 文件的保存目录。

  3. 修改 setup.py 文件,在 sqlite_inc_paths 中加上 sqlite3.h 的文件路径。

    1. sqlite_inc_paths = [ '/Bioinfo/Pipeline/SoftWare/sqlite-3.36.0/include',
    2. '/usr/include',
    3. '/usr/include/sqlite',
    4. '/usr/include/sqlite3',
    5. '/usr/local/include',
    6. '/usr/local/include/sqlite',
    7. '/usr/local/include/sqlite3',
    8. ]
  4. 配置环境。

    1. $ export LD_LIBRARY_PATH=/Bioinfo/Pipeline/SoftWare/sqlite-3.36.0/lib:$LD_LIBRARY_PATH

1.2.2 _ssl 依赖

Python3 需要引用 openssl 模块,但是 python3.7+ 在 CentOS 中要求的openssl 版本最低为 1.0.2,而 CentOS 默认的为 1.0.1(CentOS-6.x 通过 yum 源安装的 openssl 的最高版本是 1.0.1),所以需要手动更新 openssl。

:::warning Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_PARAM_set1_host(). :::

  1. # 下载
  2. wget http://www.openssl.org/source/openssl-1.1.1.tar.gz
  3. # 解压缩
  4. tar -zxvf openssl-1.1.1.tar.gz
  5. # 进入目录安装
  6. cd openssl-1.1.1
  7. # 进行配置下,自定义
  8. ./config --prefix=/Bioinfo/Pipeline/SoftWare/openssl-1.1.1 shared zlib
  9. # 编译并安装
  10. make && make install
  11. # 配置到用户环境变量,随处使用
  12. echo "export LD_LIBRARY_PATH=/Bioinfo/Pipeline/SoftWare/openssl-1.1.1/lib:$LD_LIBRARY_PATH" >> $HOME/.bashrc
  13. # 是环境变量配置生效
  14. source $HOME/.bashrc

请注意:

  1. openssl 编译(config)的时候 必须要加上 shared 参数,否者源码安装 Python 即使添加了 --with-openssl 的自定义路径,依然会导致 Could not build the ssl module! 报错!
  2. https://www.openssl.org/source/ 下载的源码 openssl-1.0.2s、openssl-1.0.2m,包括 CentOS-7.5 使用 yum 安装的最高版本的 openssl-1.0.2k 目前发现依然会导致 Could not build the ssl module ,建议从 https://www.openssl.org/source/old/ 下载 1.1.1 的源码编译安装。

1.2.3 _lzma 依赖

正常情况下,下面的方法可以解决该问题(如果您有 root 权限的话)。

  1. # For ubuntu:
  2. $ sudo apt-get install liblzma-dev
  3. # For centos:
  4. $ yum install xz-devel

普通用户可以手动安装解决:

  1. $ wget https://tukaani.org/xz/xz-5.2.5.tar.gz --no-check-certificat
  2. $ tar zvxf xz-5.2.5.tar.gz
  3. $ cd xz-5.2.5
  4. $ ./configure --prefix=/Bioinfo/Pipeline/SoftWare/xz-5.2.5
  5. $ make
  6. $ make install

最后,配置环境:

  1. $ export LD_LIBRARY_PATH=/Bioinfo/Pipeline/SoftWare/xz-5.2.5/lib:$LD_LIBRARY_PATH

1.2.4 _ctypes 依赖

在 CentOS 6.x 安装 libffi-devel 的时候出现以下问题:

  1. $ yum install libffi-devel
  2. Loaded plugins: product-id, refresh-packagekit, search-disabled-repos, security, subscription-manager
  3. This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
  4. Setting up Install Process
  5. cdrom | 4.1 kB 00:00 ...
  6. No package libffi-devel available.
  7. Error: Nothing to do

可以使用下面的方法安装:

  1. [root@log01 ~]# rpm -ivh http://mirror.centos.org/centos/6/os/x86_64/Packages/libffi-devel-3.0.5-3.2.el6.x86_64.rpm
  2. Retrieving http://mirror.centos.org/centos/6/os/x86_64/Packages/libffi-devel-3.0.5-3.2.el6.x86_64.rpm
  3. warning: /var/tmp/rpm-tmp.V9ihbu: Header V3 RSA/SHA256 Signature, key ID c105b9de: NOKEY
  4. Preparing... ########################################### [100%]
  5. 1:libffi-devel ########################################### [100%]
  6. [root@log01 ~]# rpm -qa|grep libffi
  7. libffi-3.0.5-3.2.el6.x86_64
  8. libffi-devel-3.0.5-3.2.el6.x86_64

手动的源码方法安装如下:

  1. $ wget ftp://sourceware.org/pub/libffi/libffi-3.2.1.tar.gz
  2. $ tar zvxf libffi-3.2.1.tar.gz
  3. $ ./configure --prefix=/Bioinfo/Pipeline/SoftWare/libffi-3.2.1
  4. $ make
  5. $ make install

1.2.5 pygraphviz 依赖(可选)

如果你不需要使用 pygraphviz,可以不用管这个依赖。

PyGraphviz is a Python interface to the Graphviz graph layout and visualization package. With PyGraphviz you can create, edit, read, write, and draw graphs using Python to access the Graphviz graph data structure and layout algorithms.

PyGraphviz 是 Graphviz 图形布局和可视化包的 Python 接口。 借助 PyGraphviz,您可以使用 Python 创建、编辑、读取、写入和绘制图形,以访问 Graphviz 图形数据结构和布局算法。

  1. $ /Bioinfo/Pipeline/SoftWare/Python-3.7.3/bin/pip3 install pygraphviz
  2. Collecting pygraphviz
  3. Using cached https://files.pythonhosted.org/packages/7e/b1/d6d849ddaf6f11036f9980d433f383d4c13d1ebcfc3cd09bc845bda7e433/pygraphviz-1.5.zip
  4. Installing collected packages: pygraphviz
  5. Running setup.py install for pygraphviz ... error
  6. Complete output from command /Bioinfo/Pipeline/SoftWare/Python-3.7.3/bin/python3.7 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-_zdjdg0j/pygraphviz/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-g0mz7lrq/install-record.txt --single-version-externally-managed --compile:
  7. running install
  8. Trying dpkg
  9. Failed to find dpkg
  10. Trying pkg-config
  11. Package libcgraph was not found in the pkg-config search path.
  12. Perhaps you should add the directory containing `libcgraph.pc'
  13. to the PKG_CONFIG_PATH environment variable
  14. No package 'libcgraph' found
  15. Traceback (most recent call last):
  16. File "<string>", line 1, in <module>
  17. File "/tmp/pip-install-_zdjdg0j/pygraphviz/setup.py", line 93, in <module>
  18. tests_require=['nose>=1.3.7', 'doctest-ignore-unicode>=0.1.2', 'mock>=2.0.0'],
  19. File "/Bioinfo/Pipeline/SoftWare/Python-3.7.3/lib/python3.7/site-packages/setuptools/__init__.py", line 145, in setup
  20. return distutils.core.setup(**attrs)
  21. File "/Bioinfo/Pipeline/SoftWare/Python-3.7.3/lib/python3.7/distutils/core.py", line 148, in setup
  22. dist.run_commands()
  23. File "/Bioinfo/Pipeline/SoftWare/Python-3.7.3/lib/python3.7/distutils/dist.py", line 966, in run_commands
  24. self.run_command(cmd)
  25. File "/Bioinfo/Pipeline/SoftWare/Python-3.7.3/lib/python3.7/distutils/dist.py", line 985, in run_command
  26. cmd_obj.run()
  27. File "/tmp/pip-install-_zdjdg0j/pygraphviz/setup_commands.py", line 44, in modified_run
  28. self.include_path, self.library_path = get_graphviz_dirs()
  29. File "/tmp/pip-install-_zdjdg0j/pygraphviz/setup_extra.py", line 162, in get_graphviz_dirs
  30. include_dirs, library_dirs = _try_configure(include_dirs, library_dirs, _pkg_config)
  31. File "/tmp/pip-install-_zdjdg0j/pygraphviz/setup_extra.py", line 117, in _try_configure
  32. i, l = try_function()
  33. File "/tmp/pip-install-_zdjdg0j/pygraphviz/setup_extra.py", line 72, in _pkg_config
  34. output = S.check_output(['pkg-config', '--libs-only-L', 'libcgraph'])
  35. File "/Bioinfo/Pipeline/SoftWare/Python-3.7.3/lib/python3.7/subprocess.py", line 395, in check_output
  36. **kwargs).stdout
  37. File "/Bioinfo/Pipeline/SoftWare/Python-3.7.3/lib/python3.7/subprocess.py", line 487, in run
  38. output=stdout, stderr=stderr)
  39. subprocess.CalledProcessError: Command '['pkg-config', '--libs-only-L', 'libcgraph']' returned non-zero exit status 1.
  40. ----------------------------------------
  41. Command "/Bioinfo/Pipeline/SoftWare/Python-3.7.3/bin/python3.7 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-_zdjdg0j/pygraphviz/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-g0mz7lrq/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-install-_zdjdg0j/pygraphviz/

参考:《Installation:fatal error: ‘graphviz/cgraph.h’ file not found

  1. $ wget https://graphviz.gitlab.io/pub/graphviz/stable/SOURCES/graphviz.tar.gz
  2. $ tar zvxf graphviz.tar.gz
  3. $ cd graphviz-2.40.1
  4. $ ./configure --prefix=/Bioinfo/Pipeline/SoftWare/graphviz-2.40.1
  5. $ make && make install

推荐把安装好的 graphviz 添加到环境变量,这样可以避免运行过程中出现:”pygraphviz/graphviz_wrap.c:2987:29: fatal error: graphviz/cgraph.h: No such file or directory” 无法找到头文件的异常。

  1. export PKG_CONFIG_PATH=/Bioinfo/Pipeline/SoftWare/graphviz-2.40.1/lib/pkgconfig:$PKG_CONFIG_PATH
  2. export LD_LIBRARY_PATH=/Bioinfo/Pipeline/SoftWare/graphviz-2.40.1/lib:$LD_LIBRARY_PATH
  3. export C_INCLUDE_PATH=/Bioinfo/Pipeline/SoftWare/graphviz-2.40.1/include:$C_INCLUDE_PATH
  4. export CPLUS_INCLUDE_PATH=/Bioinfo/Pipeline/SoftWare/graphviz-2.40.1/include:$CPLUS_INCLUDE_PATH

如果 graphviz 添加到环境变量, pygraphviz 的 python 包可以参考下面的方法安装:

  1. $ /Bioinfo/Pipeline/SoftWare/Python-3.7.3/bin/pip3 install --global-option=build_ext --global-option="-I/Bioinfo/Pipeline/SoftWare/graphviz-2.40.1/include" --global-option="-L/Bioinfo/Pipeline/SoftWare/graphviz-2.40.1/lib" pygraphviz
  2. /Bioinfo/Pipeline/SoftWare/Python-3.7.3/lib/python3.7/site-packages/pip/_internal/commands/install.py:207: UserWarning: Disabling all use of wheels due to the use of --build-options / --global-options / --install-options.
  3. cmdoptions.check_install_build_global(options)
  4. Collecting pygraphviz
  5. Using cached https://files.pythonhosted.org/packages/7e/b1/d6d849ddaf6f11036f9980d433f383d4c13d1ebcfc3cd09bc845bda7e433/pygraphviz-1.5.zip
  6. Installing collected packages: pygraphviz
  7. Running setup.py install for pygraphviz ... done
  8. Successfully installed pygraphviz-1.5

1.3 编译安装

第一,下载 Python 源码,解压。

  1. # 官网下载地址 https://www.python.org/downloads
  2. wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz
  3. # 解压到指定目录
  4. tar zvxf Python-3.7.3.tgz -C /usr/local/src

第二,进入解压的源码路径,编译 Python 源码。

  1. $ soft=/Bioinfo/Pipeline/SoftWare
  2. $ export PKG_CONFIG_PATH=$soft/libffi-3.2.1/lib/pkgconfig:$PKG_CONFIG_PATH
  3. $ export PKG_CONFIG_PATH=$soft/graphviz-2.40.1/lib/pkgconfig:$PKG_CONFIG_PATH
  4. $ export LD_LIBRARY_PATH=$soft/libffi-3.2.1/lib64:$LD_LIBRARY_PATH
  5. $ export LD_LIBRARY_PATH=$soft/graphviz-2.40.1/lib:$LD_LIBRARY_PATH
  6. $ export LD_LIBRARY_PATH=$soft/openssl-1.1.1/lib:$LD_LIBRARY_PATH
  7. $ export LD_LIBRARY_PATH=$soft/sqlite-3.36.0/lib:$LD_LIBRARY_PATH
  8. $ ./configure \
  9. --enable-optimizations \
  10. --prefix=/Bioinfo/Pipeline/SoftWare/Python-3.7.3 \
  11. --with-openssl=$libs/openssl-1.1.1 \
  12. CC=/Bioinfo/Pipeline/SoftWare/gcc-4.8.5/bin/gcc \
  13. CXX=/Bioinfo/Pipeline/SoftWare/gcc-4.8.5/bin/c++ \
  14. LDFLAGS="-L/Bioinfo/Pipeline/SoftWare/libffi-3.2.1/lib64 \
  15. -L/Bioinfo/Pipeline/SoftWare/graphviz-2.40.1/lib" \
  16. CPPFLAGS="-I/Bioinfo/Pipeline/SoftWare/graphviz-2.40.1/include" \
  17. PKG_CONFIG_PATH="/Bioinfo/Pipeline/SoftWare/libffi-3.2.1/lib/pkgconfig: \
  18. /Bioinfo/Pipeline/SoftWare/graphviz-2.40.1/lib/pkgconfig"
  • --enable-optimizations 是优化选项(LTO,PGO 等)加上这个 flag 编译后,性能有 10% 左右的优化,但是这会明显的增加编译时间。建议使用这个参数;
  • --prefix 声明安装路径;
  • 安装多个 python 的版本,如果不开启 --enable-shared,指定不同路径即可。当开启 --enable-shared 时,默认只有一个版本的 python。
  • python 3 编译可以使用 --with-openssl=DIR 指定 OpenSSL 安装路径进行编译的方式解决 OpenSSL 依赖,否则 make 过程可能出错。 ```shell $ make …… 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 _hashlib _ssl

Could not build the ssl module! Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_PARAM_set1_host(). LibreSSL 2.6.4 and earlier do not provide the necessary APIs, https://github.com/libressl-portable/portable/issues/381

……

  1. - `make` 过程如果出现 `ModuleNotFoundError: No module named '_ctypes'` 或者 `INFO: Could not locate ffi libs and/or headers` 参考:[https://groups.google.com/forum/#!topic/comp.lang.python/npv-wzmytzo](https://groups.google.com/forum/#!topic/comp.lang.python/npv-wzmytzo)
  2. ![image.png](https://cdn.nlark.com/yuque/0/2019/png/126032/1562923411627-eb429b96-2ec8-408e-abbb-05f4e8a320f8.png#crop=0&crop=0&crop=1&crop=1&height=316&id=VNMHL&margin=%5Bobject%20Object%5D&name=image.png&originHeight=316&originWidth=581&originalType=binary&ratio=1&rotation=0&showTitle=false&size=34010&status=done&style=none&title=&width=581)
  3. - 如果指定 `--with-openssl=DIR` 依然无法解决 ssl 模块的问题,可以参考修改 Modules/Setup.dist 文件(默认这块是注释的,放开注释即可。这块功能是开启 SSL 模块,不然会出现安装完毕后,提示找不到 ssl 模块的错误)再执行 configure,修改内容如下:
  4. ```bash
  5. # Socket module helper for SSL support; you must comment out the other
  6. # socket line above, and possibly edit the SSL variable:
  7. SSL=/usr/local/ssl
  8. _ssl _ssl.c \
  9. -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
  10. -L$(SSL)/lib -lssl -lcrypto

第三,Makefile 生后依次在当前路径执行编译和安装命令。

  1. make && make install

第四,安装完成。以上命令执行完毕,且无报错的情况下,我们将默认 python 换将切换至 3.7.3(一般不建议替换,个人建议把自定义安装的 Python bin 路径添加到 PATH 环境变量即可):

  1. # 替换系统自带的 python(不建议)
  2. mv /usr/bin/python /usr/bin/python2
  3. ln -s /Bioinfo/Pipeline/SoftWare/Python-3.7.3/bin/python3 /usr/bin/python
  4. # 添加新 Python 到 PATH 环境变量(建议)
  5. echo "export PATH=/Bioinfo/Pipeline/SoftWare/Python-3.7.3/bin:$PATH" >>~/.bashrc
  6. source ~/.bashrc

运行命令 python -V ,查看是否出现 3.7.3 的版本,出现即为安装成功。

1.4 安装 pip+setuptools

说明:Python >= 3.10 在安装时候,默认会同时安装 pip3!如果你的 python < 3.10,可以参考下面的方法安装 pip。

  1. # 下载 setuptools 和 pip 安装程序
  2. curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
  3. python3 get-pip.py

至此,CentOS Linux release 6.5 下的 python-3.7.3 全部安装完成。如果在安装过程中出现其他的报错,建议把 error 关键信息直接复制到 Google 进行检索,参考其他人的解决方法。

1.5 其他异常与解决

1.5.1 _bz2

  • 系统:CentOS Linux release 7.7.1908 (Core)
  • GCC:gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39)

在 CentOS 7 中安装 Python-3.6.9 中发现 make 过程中一直提示:“ModuleNotFoundError: No module named ‘_bz2’”,尽管 sudo yum install bzip2 bzip2-devel 已经安装了 bzip2 的依赖,问题还是不得其解。最后参考 stackoverflow 上的《Correctly building local python3, with bz2 support》,终于解决问题,下面记录一下。

手动安装 bzip2:

  1. wget https://nchc.dl.sourceforge.net/project/bzip2/bzip2-1.0.6.tar.gz
  2. tar zvxf bzip2-1.0.6.tar.gz
  3. cd bzip2-1.0.6
  4. make -f Makefile_libbz2_so # 这一步是生成 libbz2.so.1.0.6 的动态库文件
  5. make
  6. make install PREFIX=/usr/local/software/bzip2-1.0.6
  7. cp libbz2.so.1.0.6 /usr/local/software/bzip2-1.0.6/lib/

几点说明:

image.png

编译安装 Python-3.6.9:

  1. $ wget https://www.python.org/ftp/python/3.6.9/Python-3.6.9.tgz
  2. $ tar zvxf Python-3.6.9.tgz
  3. $ cd Python-3.6.9
  4. $ ./configure --enable-optimizations --prefix=/usr/local/software/python-3.6 CFLAGS="-I/usr/local/software/bzip2-1.0.6/include" LDFLAGS="-L/usr/local/software/bzip2-1.0.6/lib"
  5. $ make
  6. $ make install
  • Python-3.6.9 中的 ./configure --help 中没有 --with-openssl 参数!有点神奇,我也不知道原因。
  • 安装完成可以用 from _bz2 import BZ2Compressor, BZ2Decompressor 测试一下 _bz2 是否可用。

2. Ubuntu/Debian

2.1 安装环境

Ubuntu 20.04 + GCC 9.3.0。

  1. apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev

2.2 解决依赖

缺少库名称 安装命令
_uuid apt install uuid-dev
readline apt install libreadline-dev
_tkinter apt install python3-tk tk tk-dev
_ffi apt install libffi-dev
_curses apt install libncurses5-dev
_sqlite apt install libsqlite3-dev
_bz2 apt install libbz2-dev
_ssl apt install libssl-dev
_gdbm apt install libgdbm-compat-dev
_dbi
_zlib apt install zlib1g-dev
_lzma apt install lzma-dev liblzma-dev
_ctypes apt install libffi-dev

2.3 编译安装

Ubuntu/Debian 下 Python 编译安装的命令跟 CentOS/RedHat 是一样的,具体参考 #1.3 编译安装 一节的内容。

F. 参考资料

  1. 行者无疆-ITer,《python2.7 源码编译安装》, 博客园
  2. Scott Frazer,《How do I compile Python 3.4 with custom OpenSSL?》, Stack Overflow