Linux下使用源码包安装Python

系统管理员 2022-02-22 00:31 1598阅读 0赞

目录

准备环境:

Python的下载和安装

常见问题


准备环境:

Linux:CentOS Linux release 7.2.1511 (Core)

Python:Python-3.7.3

Python的下载和安装

CentOS 7.2 自带了Python2.7.5,我们需要另行安装Python 3.7.3。但是由于系统运行依赖于自带的Python2.7.5,因此千万不可删除系统自带的Python 版本。

20190408183441168.png

首先,我们需要登陆Python官方网站 获取 Python-3.7.3.tgz下载链接。

安装目标文件夹:/usr/local/python3

按照如下步骤进行操作:

  1. # 首先切换到下载目录 /usr/src/
  2. [root@hadoop39 src]# cd /usr/src/
  3. # 根据自己的实际需要下载相应版本的 Python 压缩包
  4. [root@hadoop39 src]# wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz
  5. # 将下载好的 Python 压缩包解压到 /usr/src/
  6. [root@hadoop39 src]# tar -zxvf Python-3.7.3.tgz
  7. # 进入解压目录
  8. [root@hadoop39 src]# cd Python-3.7.3/
  9. # 指定编译安装的目标目录
  10. [root@hadoop39 Python-3.7.3]# ./configure --prefix=/usr/local/python3
  11. # 进行编译
  12. [root@hadoop39 Python-3.7.3]# make
  13. # 进行安装
  14. [root@hadoop39 Python-3.7.3]# make install
  15. # 为python3.7可执行文件创建软连接
  16. [root@hadoop39 Python-3.7.3]# ln /usr/local/python3/bin/python3.7 /usr/sbin/python3
  17. # 直接使用 python3 命令即可启动Python-3.7.3
  18. [root@hadoop39 python3]# python3
  19. # 为 pip3 可执行文件创建软连接
  20. [root@hadoop39 Python-3.7.3]# ln /usr/local/python3/bin/pip3 /usr/sbin/pip3
  21. # 使用 pip3 安装requests 模块
  22. [root@hadoop39 Python-3.7.3]# sudo pip3 install requests

注意:如果编译出错,可以执行make clean清理编译产生的临时文件,放弃安装。

常见问题

1) 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

20190408183111443.png

原因分析:python3.7的_ssl模块依赖openssl 1.0.2 或者 libssl 1.1以上版本,LibreSSL 2.6.4以前的版本未提供必需的API,导致安装失败。

解决办法:安装新版本的LibreSSL 替代系统自带的 openssl ,这里我们安装 LibreSSL 2.8.3。

  1. # 首先切换到下载目录 /usr/src/
  2. [root@hadoop39 src]# cd /usr/src/
  3. # 根据自己的实际需要下载相应版本的 LibreSSL 压缩包
  4. [root@hadoop39 src]# wget https://ftp.openbsd.org/pub/OpenBSD/LibreSSL/libressl-2.8.3.tar.gz
  5. # 将下载好的 LibreSSL 压缩包解压到 /usr/src/
  6. [root@hadoop39 src]# tar -zxvf libressl-2.8.3.tar.gz
  7. # 进入解压目录
  8. [root@hadoop39 src]# cd libressl-2.8.3/
  9. # 指定编译安装的目标目录
  10. [root@hadoop39 libressl-2.8.3]# ./configure --prefix=/usr/local/libressl
  11. # 进行编译
  12. [root@hadoop39 libressl-2.8.3]# make
  13. # 进行安装
  14. [root@hadoop39 libressl-2.8.3]# make install
  15. # 将/usr/bin/中系统自带的openssl软链接备份
  16. [root@hadoop39 libressl-2.8.3]# mv /usr/bin/openssl /usr/bin/openssl.bak
  17. [root@hadoop39 libressl-2.8.3]# mv /usr/include/openssl /usr/include/openssl.bak
  18. # 使用libressl替换原有的openssl软链接
  19. [root@hadoop39 libressl-2.8.3]# ln -s /usr/local/libressl/bin/openssl /usr/bin/openssl
  20. [root@hadoop39 libressl-2.8.3]# ln -s /usr/local/libressl/include/openssl /usr/include/openssl
  21. [root@hadoop39 libressl-2.8.3]# cd /etc/ld.so.conf.d
  22. # 新建一个文件 libressl-2.8.3.conf
  23. [root@localhost ld.so.conf.d]# touch libressl-2.8.3.conf
  24. [root@localhost ld.so.conf.d]# echo /usr/local/libressl/lib >> libressl-2.8.3.conf
  25. # 重新加载类库
  26. [root@localhost ld.so.conf.d]# ldconfig -v
  27. # 验证openssl 版本
  28. [root@localhost ld.so.conf.d]# openssl version
  29. LibreSSL 2.8.3

此外, 在编译python之前,需要做以下环境变量配置:

  1. export LDFLAGS="-L/usr/local/libressl/lib"
  2. export CPPFLAGS="-I/usr/local/libressl/include"
  3. export PKG_CONFIG_PATH="/usr/local/libressl/lib/pkgconfig"

2) Python build finished successfully!

The necessary bits to build these optional modules were not found:

_bz2 _curses _curses_panel

_dbm _gdbm _lzma

_sqlite3 _tkinter _uuid

readline

To find the necessary bits, look in setup.py in detect_modules() for the module’s name.

20190408183301919.png

解决办法:安装缺失的依赖。

  1. sudo yum -y install bzip2-devel ncurses-devel db4-devel gdbm gdbm-devel libpcap-devel xz xz-devel libffi-devel lzma sqlite sqlite-devel tk tk-devel libuuid-devel readline-devel zlib-devel

发表评论

表情:
评论列表 (有 0 条评论,1598人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Linux-安装

    安装源码包 安装源码包,需要我们把源代码编译成可执行的二进制文件。源码包的编译用到了Linux系统里的编译器。常见的源码包一般都是用C语言开发的,因为C语言是Linux上