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

本文详细介绍了在Centos7系统中如何同时安装和使用Python2和Python3,避免卸载Python2导致系统软件命令失效的问题。通过软链接方式安装Python3,并保持Python2的正常使用,确保系统稳定运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于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 都可用。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值