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

egdjgwm8  于 2022-11-07  发布在  Linux
关注(0)|答案(5)|浏览(185)

我使用的是运行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版本?

谢谢

eqoofvh9

eqoofvh91#

最简单的办法:只需在您/home/.bashrc中添加一个别名,如下所示:

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

(我想CentOS和Linux Mint有类似的结构)
但是你可能只需要使用virtual env,here's一个链接就可以开始了。解决像这样的问题是virtual env的主要目的。

cwxwcias

cwxwcias2#

对于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
fbcarpbf

fbcarpbf3#

也许你想熟悉一下 * 替代品 *

  • alternatives* 创建、删除、维护和显示组成alternatives系统的符号链接的信息。alternatives系统是Debian alternatives系统的重新实现。

Check out this thread,我在这里快速地回顾了实现您所请求的内容的基本命令,还看了一下alternatives manpages

mkshixfv

mkshixfv4#

如果你想将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"
        ;;
owfi6suc

owfi6suc5#

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

相关问题