CentOS 7 升级 Python 3

登陆系统, 查看当前版本

$ cat /etc/centos-release
CentOS Linux release 7.8.2003 (Core)
$ python -V
Python 2.7.5
$ pip -V
pip 8.1.2 from /usr/lib/python2.7/site-packages (python 2.7)

升级 Python3

升级到最新版 3.7.8, 先决条件

yum install -y zlib zlib-devel sqlite sqlite-devel libffi-devel
yum install -y openssl openssl-devel bzip2-devel xz-devel

下载, 官网 https://www.python.org/

wget https://www.python.org/ftp/python/3.7.8/Python-3.7.8.tgz
tar -xkzvf Python-3.7.8.tgz
cd Python-3.7.8
./configure --enable-optimizations && make && make install

检查

$ python3 -V
Python 3.7.8
$ pip3 -V
pip 20.1.1 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)

设置默认为 Python3

建议不要将系统 python 命令, 指向 python3, 而是直接使用 python3. 如果还是想做映射, 可以参见如下步骤. 查看命令的位置

$ which python3
/usr/local/bin/python3
$ which pip3
/usr/local/bin/pip3
$ which python
/usr/bin/python
$ which pip
/usr/bin/pip

创建软连接

cd /usr/bin
rm -frv python pip
ln -s /usr/local/bin/python3 python
ln -s /usr/local/bin/pip3 pip

检查

$ python -V
Python 3.6.4
$ pip -V
pip 9.0.1 from /usr/local/lib/python3.6/site-packages (python 3.6)

修复 Yum

编辑 /usr/bin/yum, /usr/libexec/urlgrabber-ext-down, 修改第一行

#! /usr/bin/python
改为
#! /usr/bin/python2

安装 Jupyter

pip install jupyter
useradd henry
passwd henry
ssh henry@ip
mkdir workspace_python
nohup jupyter notebook --ip 0.0.0.0 --notebook-dir workspace_python &

配置阿里云安全组规则, 添加 TCP 8888 端口

Jupyter 兼容 python2, python3

jupyter 兼容 python2, python3, 参考Run Python 2 & 3 in Jupyter on Fedora Linux

yum install -y python-devel
pip2 install 'ipython<6.0'
python2 -m pip install ipykernel
python2 -m ipykernel install

参考文献