03 Centos7部署python3.8环境
1. 查看当前python环境
CentOS7自带的有python环境, 但是版本是2.7
- 查看Python的位置
[root@oldbody ~]# whereis python
python: /usr/bin/python /usr/bin/python2.7 /usr/lib/python2.7 /usr/lib64/python2.7 /etc/python /usr/include/python2.7 /usr/share/man/man1/python.1.gz
可以知道我们的python在/usr/bin
目录中
[root@oldbody ~]# cd /usr/bin
[root@oldbody bin]# ll python*
lrwxrwxrwx. 1 root root 7 11月 30 18:06 python -> python2
lrwxrwxrwx. 1 root root 9 11月 30 18:06 python2 -> python2.7
-rwxr-xr-x. 1 root root 7216 10月 31 2018 python2.7
python指向的是python2,python2指向的是python2.7,因此我们可以装个python3,然后将python指向
python3,然后python2指向python2.7,那么两个版本的python就能共存了。
2. 安装依赖包
[root@oldbody ~]# yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make
运行了以上命令以后,就安装了编译python3所用到的相关依赖
3. 下载Python3源码包并解压
[root@oldbody ~]# wget http://npm.taobao.org/mirrors/python/3.8.0/Python-3.8.0.tgz
# 如果提示wget未找到命令, 那么就先使用yum安装wget
[root@oldbody ~]# yum install wget -y
# 解压源码包
[root@oldbody ~]# tar -zxvf python-3.8.0
4. 编译Python3源码包
#进入解压后的目录,依次执行下面命令进行手动编译
[root@oldbody ~]# cd python-3.8.0
# 指定python3的安装路径
[root@oldbody ~]# ./configure prefix=/usr/local/python3
# 编译安装
[root@oldbody ~]# make && make install
5. 添加软链接
[root@oldbody ~]# ln -s /usr/local/python3/bin/python3.8 /usr/local/bin/python3
[root@oldbody ~]# ln -s /usr/local/python3/bin/pip3 /usr/local/bin/pip3
配置pip源,提高下载速度
[root@oldbody ~]# mkdir ~/.pip && touch ~/.pip/pip.conf
[root@oldbody ~]# vim ~/.pip/pip.conf
内容如下
[global]
timeout=6000
index-url=http://pypi.douban.com/simple
trusted-host=pypi.douban.com
6. 更改yum配置
因为其要用到python2才能执行,否则会导致yum不能正常使用
[root@oldbody ~]# vi /usr/bin/yum
把#! /usr/bin/python修改为#! /usr/bin/python2
[root@oldbody ~]# vi /usr/libexec/urlgrabber-ext-down
把#! /usr/bin/python 修改为#! /usr/bin/python2
7. 测试
[root@oldbody bin]# python3 -V
Python 3.8.0
[root@oldbody bin]# python2 -V
Python 2.7.5
[root@oldbody bin]# pip3 -V
pip 20.3.1 from /usr/local/python3/lib/python3.8/site-packages/pip (python 3.8)