Python|Linux 安装 Python 3.10+ 环境步骤

打印 上一主题 下一主题

主题 556|帖子 556|积分 1668

Step 1:在 Linux 中安装前置依赖环境

  1. sudo yum install wget zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make
  2. zlib zlib-devel libffi-devel -y
复制代码
  未执行此步骤的表现|如果没有安装这些依赖,则会出现报错类似如下找不到模块的报错信息。
  1. ModuleNotFoundError: No module named '_bz2'
复制代码
这是由于 Python 的编译时,如果发现体系中没有 bz2 的相关依赖,旧不会在 <ython安装地址>/lib/<ython版本>/lib-dynload 路径下生成 bz2 的 so 文件,即没有安装 bz2 相关模块。在使用如许的 Python 环境时,如果导入依赖 bz2 模块的包,就会因 _bz2 模块不存在而报错。
  Step 2:编译、安装新版本的 openssl

参考资料:https://blog.csdn.net/weixin_44894162/article/details/126342591
参考资料:https://blog.csdn.net/bo_self_effacing/article/details/123628224
在 Python 3.10 及之后的版本,还必要安装新版本的 openssl,Linux 默认版本无法满足要求。
   未执行此步骤的表现|如果没有安装新版本的 openssl,那么在使用 pip 安装包时,旧会出现类似如下的报错。如出现报错此报错,则必要先重新执行 ./configure 后,再重新编译、安装 Python 环境。
  1. WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
复制代码
Step 1|安装 openssl 依赖的环境
  1. sudo yum install gcc libffi-devel zlib* openssl-devel
复制代码
Step 2|下载、解压新版本的 openssl 安装包(安装 1.1.1N 版本即可)
  1. wget https://www.openssl.org/source/openssl-1.1.1n.tar.gz
  2. tar -zxvf openssl-1.1.1n.tar.gz
  3. cd openssl-1.1.1n
复制代码
Step 3|设置 openssl
  1. ./config --prefix=/usr/local/openssl
复制代码
  相关报错及处置惩罚方法|如果出现报错如下:
  1. Can't locate IPC/Cmd.pm in @INC (@INC contains: /data/py_src/openssl-3.0.1/util/perl /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 . /data/py_src/openssl-3.0.1/external/perl/Text-Template-1.56/lib) at /data/py_src/openssl-3.0.1/util/perl/OpenSSL/config.pm line 18.
  2. BEGIN failed--compilation aborted at /data/py_src/openssl-3.0.1/util/perl/OpenSSL/config.pm line 18.
  3. Compilation failed in require at /data/py_src/openssl-3.0.1/Configure line 23.
  4. BEGIN failed--compilation aborted at /data/py_src/openssl-3.0.1/Configure line 23.
复制代码
则执行:
  1. yum install -y perl-CPAN
复制代码
  1. perl -MCPAN -e shell
复制代码
在 perl 启动后执行:
  1. install IPC/Cmd.pm
复制代码
安装完成后,重新继续安装 openssl 即可。
  参考资料:https://blog.csdn.net/qq_44768464/article/details/136076394
  Step 4|编译、安装 openssl
  1. make
复制代码
  1. make
  2. install
复制代码
如果必要的话,可以将安装的映射到 use/bin 中:
  1. sudo ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
复制代码
映射后,可查看安装的 openssl 的版本:
  1. openssl version
复制代码
表现如下内容即可:
  1. OpenSSL 1.1.1n  15 Mar 2022 (Library: OpenSSL 1.1.1c  28 May 2019)
复制代码
  相关报错及处置惩罚方法|如果出现报错如下:
  1. openssl: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or directory
复制代码
在 /usr/local/openssl/lib 和 /usr/local/openssl/lib64/ 路径中,探求是否存在缺失的依赖包,将它们重新定向到 /usr/lib 或 /usr/lib64 路径下即可。
  1. sudo ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/lib/libssl.so.1.1
  2. sudo ln -s /usr/local/openssl/lib64/libssl.so.1.1 /usr/lib64/libssl.so.1.1
  3. sudo ln -s /usr/local/openssl/lib/libcrypto.so.1.1 /usr/lib/libcrypto.so.1.1
  4. sudo ln -s /usr/local/openssl/lib64/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1
复制代码
参考资料:https://blog.csdn.net/qq_45742250/article/details/134876045
  Step 3:编译、安装 Python

Step 1:下载必要安装的 Python 步伐源码。找到目标版本,选择 Gzipped source tarball,复制目标版本源码的下载地址并下载到本地:
  1. wget https://www.python.org/ftp/python/3.10.6/Python-3.10.6.tgz
复制代码
Step 2:解压 Python 步伐源码到当前目次
  1. tar -xvf Python-3.10.6.tgz
复制代码
上述命令会将 Python-3.8.5.tgz 中的文件解压到 Python-3.8.5 文件夹下
Step 3:进入解压后的目次
  1. cd Python-3.10.6
复制代码
Step 4:设置 Python 源码编译后的安装路径
  1. ./configure --prefix=/usr/local/python3.10 --with-openssl=/usr/local/openssl --with-openssl-rpath=auto
复制代码
这个命令必要执行 5 - 20 秒左右,如果打印的日志末尾为如下内容,则说明执行乐成:
  1. creating Modules/Setup.local
  2. creating Makefile
  3. If you want a release build with all stable optimizations active (PGO, etc),
  4. please run ./configure --enable-optimizations
复制代码
往上看一点日志,查抄 openssl 相关的四个查抄项结果是否正常,预期如下:
  1. checking for openssl/ssl.h in /usr/local/openssl... yes
  2. checking whether compiling and linking against OpenSSL works... yes
  3. checking for --with-openssl-rpath... auto
  4. checking whether OpenSSL provides required APIs... yes
复制代码
Step 5:编译、安装 Python
  1. make
复制代码
  1. make
  2. install
复制代码
这个命令必要执行 3 - 10 分钟左右,如果打印的日志末尾为如下内容,则说明执行乐成:
  1. Processing /tmp/tmp29iyxren/setuptools-47.1.0-py3-none-any.whl
  2. Processing /tmp/tmp29iyxren/pip-20.1.1-py2.py3-none-any.whl
  3. Installing collected packages: setuptools, pip
  4.   WARNING: The script easy_install-3.10 is installed in /usr/local/python3.10/bin' which is not on PATH.
  5.   Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  6.   WARNING: The scripts pip3 and pip3.10 are installed in '/usr/local/python3.10/bin' which is not on PATH.
  7.   Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  8. Successfully installed pip-20.1.1 setuptools-47.1.0
复制代码
其中的告诫是由于没有将 Python 环境添加到 PATH 中,不影响正常使用。
   相关报错及处置惩罚方法|如果出现如下报错信息:
  1. *** WARNING: renaming "_ssl" since importing it failed: /data/py_src/Python-3.10.6/build/lib.linux-x86_64-3.10/_ssl.cpython-310-x86_64-linux-gnu.so: undefined symbol: OPENSSL_sk_num
  2. *** WARNING: renaming "_hashlib" since importing it failed: /data/py_src/Python-3.10.6/build/lib.linux-x86_64-3.10/_hashlib.cpython-310-x86_64-linux-gnu.so: undefined symbol: HMAC_CTX_new
复制代码
说明在 /usr/local/openssl/lib 和 /usr/local/openssl/lib64/ 路径以及 openssl 的路径中存在多个版本相互冲突。将 openssl 文件夹以及链接全部删除,重新解压缩、设置、编译、安装 openssl 后,再重新设置、编译、安装 Python。
  Step 6|执行刚才指定的安装目次中的 bin 路径中的 python3,添加 --version 参数查看 Python 版本:
  1. /usr/local/python3.10/bin/python3 -- version
复制代码
如果打印类似如下日志,则说明安装正常:
  1. Python 3.10.6
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

大号在练葵花宝典

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表