CentOS7下安装Python3和PIP3

为了方便以后编译,所以整合了下配置流程

先将ssh调整到不超时,方便后续配置,在 /etc/ssh/ssh_config 最后加上以下内容

TCPKeepAlive yes
ClientAliveInterval 500
ClientAliveCountMax 3

然后重启ssh服务,如果还不行,就在xshell将会话保活包的间隔降低并开启空闲状态下保活,否则在编译或安装过程中中断会非常麻烦

systemctl reload sshd
systemctl restart sshd

先查看是否安装了Python3

python3 -V

如果没有安装,则先安装依赖包

yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make

从官网下载Python3(具体版本可以根据个人需要修改),解压编译和安装

wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tar.xz
tar -xvJf  Python-3.6.2.tar.xz
cd Python-3.6.2
./configure prefix=/usr/local/python3
make && make install

添加软链

ln -s /usr/local/python3/bin/python3 /usr/bin/python3

查看是否安装成功

python3 -V

将依赖修改为Python2

vim /usr/bin/yum
将第一行#! /usr/bin/python修改为#! /usr/bin/python2
保存并退出
vim /usr/libexec/urlgrabber-ext-down
将第一行#! /usr/bin/python 修改为#! /usr/bin/python2
保存并退出

安装PIP3

yum -y install epel-release
yum install python3-pip

添加软链

ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

查看软链

ll python*

安装完成

 批量更新库可以使用pip-review

pip3 install pip-review
自动更新
pip-review --auto --use-feature=2020-resolver
手动更新
pip-review --local --interactive --use-feature=2020-resolver
若安装某些库时报错缺少Python.h,则
sudo yum install python36-devel

 生成requirements

pip3 freeze > requirements.txt
pipreqs /home/project/location

 使用requirements

pip3 install -r requirements.txt

感谢以下GEEKS:

linux-Centos7安装python3并与python2共存

https://www.cnblogs.com/JahanGu/p/7452527.html

更新时间:2019.10.09

原文地址:https://www.cnblogs.com/Pyrokine/p/9309995.html