1. 从Python官网到获取Python3的包, 切换到目录/usr/local/srccd /usr/local/src#wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz2. 使用命令如下命令进行解压缩: tar -xzf Python-3.6.3.tgz3. 在/usr/local路径下创建目录--python3, 为第4步的安装目录$mkdir /usr/local/python34. 编译安装(会自动安装pip-7.1.2)$cd /usr/local/src/Python-3.6.3#./configure --prefix=/usr/local/python3#make all#make install5. 进入安装的绝对路径,检查是否安装成功$ /usr/local/python3/bin/python3.6 -V Python 3.6.36.查看环境变量,启动python时默认按照PATH的顺序进行查找,在/usr/bin中的python此时为Python2.6的。 $echo $PATH /usr/lib/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/gordon/bin7.修改软连接,启动python时指向python3.6 修改软连接 ln -s /usr/local/python3/bin/python3 /usr/bin/python3
这里是为了python2与python3共存,所以建立的软连接是python3,如果只保留python3,那就直接备份python2,但是修改有yum就不能正常使用了,需要修改 yum的配置文件,
指定python2.6.6
操作如下:
1 备份pythonmv /usr/bin/python /usr/bin/python2.6.6
2 修改软连接 ln -s /usr/local/python3/bin/python3 /usr/bin/python
3 升级完python之后,yum就不好用了,需要修改/usr/bin/yum,改为使用python2.6.6 #!/usr/bin/python2.6.6
8为了只用时输入方便可以添加alias
[root@mail ~]# cd ~[root@mail ~]# cat .bashrc # .bashrc# User specific aliases and functionsalias rm='rm -i'alias cp='cp -i'alias mv='mv -i'alias py='/usr/bin/python3'alias python3='/usr/bin/python3'# Source global definitionsif [ -f /etc/bashrc ]; then . /etc/bashrcfi
[root@mail ~]# source .bashrc
9. 验证默认是否启动python3.6$ python3 -V Python 3.6.3