Linux CentOS 7,如何将 Python2.7 设置为默认 Python 版本?

Posted

技术标签:

【中文标题】Linux CentOS 7,如何将 Python2.7 设置为默认 Python 版本?【英文标题】:Linux CentOS 7, how to set Python2.7 as default Python version? 【发布时间】:2017-11-27 12:56:58 【问题描述】:

我正在使用运行 Linux CentOS 7 的笔记本电脑。 我安装了Python2.7,然后我安装了Anaconda,它安装了Python3.5.2

我希望我的系统默认使用 Python2.7,但如果我从终端输入 python,它会从 Anaconda 启动 Python3.5.2:

[davide@opennet-33-58 ~]$ python
Python 3.5.2 |Anaconda custom (64-bit)| (default, Jul  2 2016, 17:53:06) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

我尝试删除 Python3,但它仍然存在...

如何将 Python2.7 设置为我机器上的默认 Python 版本?

谢谢

【问题讨论】:

这看起来像 CentOS6 而不是 CentOS7,因为 GCC 是 4.4.7。你能确认一下吗? @MattSchuchard 这是 CentOS Linux 版本 7.3.1611(核心) 【参考方案1】:

对于 RHE/Centos。 如果您没有它们,您将需要启用正确的 repo。(对于我的情况如下)

subscription-manager repos --enable=rhel-6-server-optional-rpms

subscription-manager repos --enable=rhel-server-rhscl-6-rpms

然后你安装

yum install scl-utils
yum install centos-release-scl-rh  (only for Centos)
yum install python27  (or any version you need to install)

现在新的python已经安装好了,你必须默认启用它

scl enable python27 bash (with this command will be default until you logout,is good to test the changes)

保留更改 您应该在 /etc/profile.d/ 下创建一个脚本

#!/bin/bash
source scl_source enable python27

【讨论】:

【参考方案2】:

也许您想熟悉替代品

alternatives 创建、删除、维护和显示有关构成替代系统的符号链接的信息。替代系统是 Debian 替代系统的重新实现。

Check out this thread 在这里我快速浏览了基本命令以实现您的要求,并查看alternatives manpages

【讨论】:

【参考方案3】:

最简单的方法:只需在 /home/.bashrc 中添加一个别名,例如:

alias python="/usr/bin/python3.5"

(我认为 CentOS 的结构与 Linux Mint 相似)

但是您可能应该只使用虚拟环境,here's 一个链接来帮助您入门。解决此类问题是虚拟环境的主要目的。

【讨论】:

我已经尝试过,但不适用于脚本。像python script.py 这样的东西总是会使用 python3.5【参考方案4】:

如果您想将 python2.7 设置为所有用户的默认 Python,请将此行添加到 /etc/profile.d/python_alias.sh(如果文件不存在则创建该文件):

alias python="/usr/bin/python2.7"

如果您只想将 python2.7 设置为某些用户的默认 Python,请将上面的行更改为:

case "$(whoami)" in
    <USER1>|<USER2>)
        alias python="/usr/bin/python2.7"
        ;;

【讨论】:

谢谢,但它不适用于像python script.py这样的脚本【参考方案5】:

将python2.7链接到python

sudo ln -fs /usr/bin/python2.7 /usr/bin/python

这是我的例子:

$ python
Python 3.5.2 (default, Sep 14 2017, 22:51:06) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

$ ls -l /usr/bin/python3.5
-rwxr-xr-x 2 root root 4456240 Sep 18  2017 /usr/bin/python3.5
$ ls -l /usr/bin/python
lrwxrwxrwx 1 root root 18 Dec  3 13:52 /usr/bin/python -> /usr/bin/python3.5
$ 
$ sudo ln -sf /usr/bin/python2.7 /usr/bin/python
$ ls -l /usr/bin/python
lrwxrwxrwx 1 root root 18 Dec  3 13:52 /usr/bin/python -> /usr/bin/python2.7
$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

【讨论】:

谢谢,我试过了,但它说:ln: failed to create symbolic link ‘/usr/bin/python’: File exists sudo rm /usr/bin/python 对您的系统非常非常危险!

以上是关于Linux CentOS 7,如何将 Python2.7 设置为默认 Python 版本?的主要内容,如果未能解决你的问题,请参考以下文章

CentOS 7从Python 2.7升级至Python3.6.1

hyper-v中搭建本地服务器linux(CentOS 7)的python环境

centos 中如何将python更新到最新的版本

Linux安装Python3.7(Centos 7)

Linux-Centos7安装python3并与python2共存

linux-Centos7安装python3并与python2共存