我正在使用运行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版本?
谢谢
对于THE / 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
也许你想熟悉替代品
替代方案创建,移除,维护和显示有关包含替代系统的符号链接的信息。替代系统是Debian替代系统的重新实现。
Check out this thread我快速浏览基本命令以实现您的要求,并查看alternatives manpages
最简单的方法:只需在/home/.bashrc中添加一个别名,如:
alias python="/usr/bin/python3.5"
(我想CentOS的结构与Linux Mint相似)
但你可能应该只使用虚拟环境,here's链接来帮助你入门。解决像这样的问题是虚拟环境的主要目的。
如果要将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"
;;
将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.
>>>