Centos安装Python各版本解释器并配置pip

时间:2022-10-26 17:08:19

Centos7.3安装Python3.7

Python3.7貌似又多了新的依赖,所以按照安装之前的套路安装在配置pip阶段就会出问题,比如:

ModuleNotFoundError: No module named '_ctypes'

或者报一个缺少setuptools包的错误,导致pip配不上,这才有了这篇博客,在解决问题后,将整理后的安装步骤列出来,供大家参考。淡少扯,开干!

环境

Centos7.4   # 阿里云服务器
Python3.7.0

下载相关依赖

yum groupinstall 'Development Tools' && yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel && yum -y epel-release
yum update
yum upgrade
yum dist-upgrade
yum install build-essential python-dev python-setuptools python-pip python-smbus -y
yum install build-essential libncursesw5-dev libgdbm-dev libc6-dev
yum install zlib1g-dev libsqlite3-dev tk-dev
yum install libssl-dev openssl
yum install libffi-dev
yum -y install gcc gcc-c++ zlib zlib-devel libffi-devel
yum -y install gcc kernel-devel kenel-headers make bzip2

遇到选择,一路y就行了

下载Python3.7的tar包

我们将tar包下载到 /usr/local/python,并且,该Python目录将是我们以后下载其他的Python解释器的安装目录,

mkdir /usr/local/python && cd /usr/local/python    # 创建Python目录并切换到该目录下
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz    # download python3.7.0

解压tar包

tar -xvf Python-3.7.0.tar.xz 
rm -rf Python-3.7.0.tar.xz     # 将无用的tar包干掉

执行安装

ps:源码安装一般分为:

  1. 配置(configure)
  2. 编译(make)
  3. 安装(make install)
cd Python-3.7.0/    # 切换到解压后的目录内
./configure --prefix=/usr/local/python/python37 --enable-optimizations    # 配置
make && make install    # 编译后安装

此时,我们已经成功的将Python3.7安装到 /usr/local/python/python37 目录下了

配置解释器和pip的环境变量

ln -s /usr/local/python/python37/bin/python3 /usr/bin/python37    # 将解释器的环境变量名配置为Python37
ln -s /usr/local/python/python37/bin/pip3 /usr/bin/pip37    # pip的环境变量名配置为pip37

ps:因为不想动yum头对Python的依赖,这里我们在使用的时候,都用37为结尾的就行了

测试

[root@cs Python-3.7.0]# python37 -V
Python 3.7.0
[root@cs Python-3.7.0]# pip37 -V
pip 10.0.1 from /usr/local/python/python37/lib/python3.7/site-packages/pip (python 3.7)

that's all