CentOS 7 升级 Python 3

该方案已不推荐, 建议查看简易通用的 Python 环境搭建方案

登陆系统, 查看当前版本 (补充查看系统的发行版本)

$ python -V
Python 2.7.5
$ pip -V
pip 9.0.1 from /usr/lib/python2.7/site-packages (python 2.7)

如果已经安装了老版本的 python3, 需要卸载, 执行 yum erase python3.

升级 Python3

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

yum install -y libX11 libXext 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-shared --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)

报错

cp libpython3.7m.so.1.0 /usr/local/lib64/
ldconfig /usr/local/lib/

设置默认为 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.7.8
$ pip -V
pip 20.1.1 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)

修复 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

参考文献

相关推荐