Centos7 在不卸载python2的前提下 安装python3

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhen_6137/article/details/82843525

由于Centos7的linux系统有很多软件命令依赖于系统自带的python2,比如yum,卸载python2会造成yum不可用,所以没有必须卸载python2, 可以通过软链接的方式安装python3, 再说 现在还有不少的python的第三库没有支持python3。

输入python -V 或python --version, 查看系统自带的python的版本。

[root@zzf ~]# python -V
Python 2.7.5
[root@zzf ~]# python --version
Python 2.7.5
[root@zzf ~]# 

输入which python ,查看python安装位置,一般是位于/usr/bin/python目录下。

[root@zzf ~]# which python
/usr/bin/python

[root@zzf ~]# 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

通过下面的命令可以看到yum是用到了系统默认的python2.7.5:

[root@zzf Python-3.6.2]# cat /usr/bin/yum
#!/usr/bin/python
import sys
try:
    import yum
except ImportError:
    print >> sys.stderr, """\
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:

   %s

Please install a package which provides this module, or
verify that the module is installed correctly.

It's possible that the above module doesn't match the
current version of Python, which is:
%s

If you cannot solve this problem yourself, please go to 
the yum faq at:
  http://yum.baseurl.org/wiki/Faq
  
""" % (sys.exc_value, sys.version)
    sys.exit(1)

sys.path.insert(0, '/usr/share/yum-cli')
try:
    import yummain
    yummain.user_main(sys.argv[1:], exit_code=True)
except KeyboardInterrupt, e:
    print >> sys.stderr, "\n\nExiting on user cancel."
    sys.exit(1)

 建议使用python的virtualenv来配置各个不同的python环境,好处显而易见的。

下面安装python3 ,安装依赖包

yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

然后根据需求下载Python3,我下载的是Python3.6.6

[root@zzf ~]#  wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tar.xz
[root@zzf ~] mkdir /usr/local/python3
[root@zzf ~] mv Python-3.6.6.tar.xz /usr/local/python3
[root@zzf ~] cd /usr/local/python3
[root@zzf python3] tar -xvJf Python-3.6.6.tar.xz
[root@zzf python3] cd Python-3.6.6
[root@zzf Python-3.6.6] ./configure --prefix=/usr/local/python3
[root@zzf Python-3.6.6] make&&make install

  最后一步 创建软链接

[root@zzf Python-3.6.6] ln -s /usr/local/python3/bin/python3 /usr/bin/python3
[root@zzf Python-3.6.6] ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

 输入python3 -V 或 python3 --version

[root@zzf Python-3.6.2]# python3 -V
Python 3.6.2
[root@zzf Python-3.6.2]# python3 --version
Python 3.6.2

安装成功,python2 和python3 都可用。

猜你喜欢

转载自blog.csdn.net/zhen_6137/article/details/82843525
今日推荐