保留python2
找到python所在位置,把python指向python2.7备份
[root@sun /usr/bin]# cd ~
[root@sun ~]# 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
[root@sun ~]# cd /usr/bin/
[root@sun /usr/bin]# ll python*
lrwxrwxrwx. 1 root root 7 2019-06-19 23:17 python -> python2
lrwxrwxrwx. 1 root root 9 2019-06-19 23:17 python2 -> python2.7
-rwxr-xr-x. 1 root root 7216 2018-10-31 07:46 python2.7
[root@sun /usr/bin]# mv python python.bak
[root@sun /usr/bin]# ll python*
下载安装python3
python下载地址:https://www.python.org/ftp/python/
# 在usr/local/下创建python3文件夹,并下载python3
[root@sun /usr/bin]# cd /usr/local/
[root@sun /usr/local]# mkdir python3
[root@sun /usr/local]# cd python3/
[root@sun /usr/local/python3]# wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz
[root@sun /usr/local/python3]# tar -xvf Python-3.6.8.tgz
# 进入文件, 指定安装目录和编译安装
[root@sun /usr/local/python3]# cd Python-3.6.8/
[root@sun /usr/local/python3/Python-3.6.8/]# ./configure --prefix=/usr/local/python3
[root@sun /usr/local/python3/Python-3.6.8]# make && make install
# 如果报zlib错
执行
mv /usr/bin/python.bak /usr/bin/python
yum install zlib* -y
mv /usr/bin/python /usr/bin/python.bak
再编译安装
# 添加软连接
[root@sun /usr/local/python3/Python-3.6.8]# ln -s /usr/local/python3/bin/python3 /usr/bin/python
修改yum配置
yum安装依赖于python2,
[root@sun /usr/local/python3]# sed -i 's#/usr/bin/python#/usr/bin/python2#g' /usr/bin/yum
[root@sun /usr/local/python3]# sed -i 's#/usr/bin/python#/usr/bin/python2#g' /usr/libexec/urlgrabber-ext-down
修改pip配置
[root@sun /usr/local/python3]# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip
pip源
mkdir ~/.pip
cd ~/.pip && touch pip.conf
cat >pip.conf<<EOF
[global]
index-url = http://pypi.douban.com/simple
[install]
use-mirrors =true
mirrors =http://pypi.douban.com/simple/
trusted-host =pypi.douban.com
EOF
window下修改
# 1、C:\Users\电脑用户\AppData\Roaming 文件夹中
# 2、新建 pip 文件夹并在文件夹中新建 pip.ini 配置文件
# 3、新增 pip.ini 配置文件内容
[global]
index-url = http://pypi.douban.com/simple
[install]
use-mirrors =true
mirrors =http://pypi.douban.com/simple/
trusted-host =pypi.douban.com
脚本文件
下载python3.6.8
yum install zlib* -y
mv /usr/bin/python /usr/bin/python.bak
mkdir /usr/local/python3
cd /usr/local/python3
wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz
tar -xf Python-3.6.8.tgz
cd Python-3.6.8/
./configure --prefix=/usr/local/python3
make && make install
ln -s /usr/local/python3/bin/python3 /usr/bin/python
sed -i 's#/usr/bin/python#/usr/bin/python2#g' /usr/bin/yum
sed -i 's#/usr/bin/python#/usr/bin/python2#g' /usr/libexec/urlgrabber-ext-down
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip
mkdir ~/.pip
cd ~/.pip && touch pip.conf
cat >pip.conf<<EOF
[global]
index-url = http://pypi.douban.com/simple
[install]
use-mirrors =true
mirrors =http://pypi.douban.com/simple/
trusted-host =pypi.douban.com
EOF