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

时间:2017-06-23 21:11:40

标签: python linux centos centos7

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

我希望我的系统默认使用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版本?

由于

5 个答案:

答案 0 :(得分:0)

对于RHE / Centos。 如果你没有,你需要启用正确的回购。(我的情况如下)

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

答案 1 :(得分:0)

也许您想熟悉替代

alternative 创建,删除,维护和显示有关包含备选系统的符号链接的信息。替代系统是Debian替代系统的重新实现。

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

答案 2 :(得分:-1)

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

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

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

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

答案 3 :(得分:-1)

将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.
>>> 

答案 4 :(得分:-1)

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

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="mystyle">
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc eu nunc nec tortor lobortis euismod at in nunc. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Sed ac turpis laoreet, placerat est nec, vehicula
  magna. Vivamus volutpat enim eget neque convallis, elementum accumsan tortor viverra. Morbi faucibus, lorem in laoreet malesuada, felis orci consectetur magna, eget convallis urna nisl at diam. Duis sapien dui, interdum nec ipsum a, porttitor tincidunt
  dolor. Donec ullamcorper eros ac elit pharetra malesuada. Duis placerat ullamcorper libero nec volutpat. Phasellus fermentum eget erat non posuere. Donec orci est, rhoncus a semper et, volutpat sed nisl. Morbi dictum lorem sed justo eleifend congue.
</div>

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

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