「Python|环境安装|Linux」CentOS 7 安装指定版本的python环境

Table of Contents

Step 1: 更新 Python 需要的安装依赖

由于python编译和安装过程中需要使用到其他工具, 如果这些工具依赖版本过低可能会导致编译安装不成功;或者安装成功后实际运行中缺少包会包冲突的问题,所以需要先更新相关依赖。

1$ sudo yum -y install epel-release 2$ sudo yum -y update 3 4$ sudo yum -y groupinstall "Development Tools" 5$ sudo yum -y install openssl-devel bzip2-devel libffi-devel xz-devel 6 7$ gcc --version 8gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44) 9Copyright (C) 2015 Free Software Foundation, Inc. 10This is free software; see the source for copying conditions. There is NO 11warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Step 2: 下载目标版本的Python压缩包

在Linux系统上, python的安装过程为: 下载压缩包, 解压, 编译, 安装。所以我们需要先从官网下载目标版本的压缩包。如果存在网络过慢, 则可以从国内镜像网站下载。

1$ sudo yum -y install wget 2$ wget https://www.python.org/ftp/python/3.10.5/Python-3.10.5.tgz 3# 官网下载速度过慢的情况,可以使用国内阿里镜像下载 4# 淘宝镜像 http://npm.taobao.org/mirrors/python/ 5$ wget https://registry.npmmirror.com/-/binary/python/3.10.5/Python-3.10.5.tgz 6 7 8# 解压 9$ tar xvf Python-3.10.5.tgz 10 11# 切换到安装包解压目录 12$ cd Python-3.10*/

Step 3: 编译python并安装

1# 运行编译脚本 2 3$ ./configure --enable-optimizations 4 5$ sudo make altinstall

Step 4: 查看python版本, 检查是否安装成功

1$ python3.10 --version 2Python 3.10.5 3 4$ pip3.10 --version 5pip 22.0.4 from /usr/local/lib/python3.10/site-packages/pip (python 3.10)

附: 修改pip安装源为国内镜像源

1$ vim /etc/pip.conf 2$ vim ~/.pip/pip.conf # mkdir ~/.pip 3$ vim ~/.config/pip/pip.conf # mkdir ~/.config
1# pip.conf文件内容 2[global] 3index-url = https://pypi.tuna.tsinghua.edu.cn/simple 4[install] 5trusted-host = https://pypi.tuna.tsinghua.edu.cn # trusted-host 此参数是为了解决地址不受信任问题

附: pycurl导包问题

问题描述

运行tornado项目时需要使用pycurl. 如果安装pycurl时没有指定与编译时相同的ssl选项, 则会导致冲突.

1pycurl: libcurl link-time ssl backend (nss) is different from compile-time ssl backend (none/other)

解决方案: 切换 pycurl 的ssl库为nss后重新安装

$ sudo -s # 如果是使用sudo安装包或启动命令的话, 需要先执行该命令后修改PY_SSL_LIBRARY
$ pip uninstall pycurl
$ export PYCURL_SSL_LIBRARY=nss
$ pip install pycurl

好书推荐:

好课推荐:

写文不易,如果对你有帮助的话,来一波点赞、收藏、关注吧~👇

Mastodon