如何在 CentOS 上将 Python3.5.2 设置为默认 Python 版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45542690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:06:27  来源:igfitidea点击:

How to set Python3.5.2 as default Python version on CentOS?

pythonpython-3.xbashcentospython-3.5

提问by muaaz

Is there a way to set the Python 3.5.2 as the default Python version on CentOS 7? currently, I have Python 2.7 installed as default and Python 3.5.2 installed separately.

有没有办法将 Python 3.5.2 设置为 CentOS 7 上的默认 Python 版本?目前,我默认安装了 Python 2.7,并分别安装了 Python 3.5.2。

I used the following commands

我使用了以下命令

mv /usr/bin/python /usr/bin/python-old
sudo ln -fs /usr/bin/python3 /usr/bin/python

but after that yumgives the error.

但在那之后yum给出了错误。

-bash: /usr/bin/yum: /usr/bin/python: bad interpreter: No such file or directory

is there something I'm missing here?

有什么我在这里想念的吗?

NOTE: its the similar but opposite question of Linux CentOS 7, how to set Python2.7 as default Python version?

注意:它与Linux CentOS 7类似但相反的问题,如何将 Python2.7 设置为默认 Python 版本?

回答by Liam

If this

如果这

sudo ln -fs /usr/bin/python3.5 /usr/bin/python

doesn't work (it should)

不起作用(应该)

you could just add an alias into your /home/.bashrcwith this command:

您可以/home/.bashrc使用以下命令将别名添加到您的:

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

and if this does not work either you should just use virtual env. Read this pageto get started.

如果这也不起作用,您应该只使用虚拟环境。阅读此页面以开始使用。

回答by OldFart

I would suggest using alternativesinstead.

我建议改用替代品

As super-user (root) run the following:

作为超级用户 ( root) 运行以下命令:

# Start by registering python2 as an alternative
alternatives --install /usr/bin/python python /usr/bin/python2 50

# Register python3.5 as an alternative
alternatives --install /usr/bin/python python /usr/bin/python3.5 60

# Select which Python version to use
alternatives --config python

The last command will ask you to choose between registered/installed alternatives.

最后一个命令将要求您在注册/安装的替代品之间进行选择。

As always, well most of the time anyways, you can check out the manual (linux man pages) using this simple command

与往常一样,无论如何,在大多数情况下,您可以使用这个简单的命令查看手册(linux 手册页

man alternatives

Note:

笔记:

Altho this answer refers to/make use of specific Pythonversions, the alternativescommand, it's concepts and uses remain the same regardless of version numbers. It is strongly suggested that you read/learn more about the alternativescommand in order to understand how it can help you better manage and use your system. Also, there is a good chance that some will correct bad/unusual practices currently in use on their machines. I see it with a great majority of people which i introduce to the concept. Here is a link to a very good and simple explanation of the alternativescommand.

尽管这个答案是指/使用特定的Python版本,alternatives命令,但无论版本号如何,它的概念和用途都保持不变。强烈建议您阅读/了解有关替代命令的更多信息,以了解它如何帮助您更好地管理和使用您的系统。此外,有些人很有可能会纠正目前在他们的机器上使用的不良/异常做法。我和我介绍这个概念的大多数人都看到了它。这是一个非常好的和简单的替代命令解释的链接

回答by yonga springfield

As the question goes, Linux CentOS 7, how to set Python3.5.2 as default Python version?

问题来了,Linux CentOS 7,如何设置Python3.5.2为默认Python版本?

Will like to complement @OldFart's answer( Unforunately, can't comment else I would have).

想补充@OldFart 的回答(不幸的是,我无法评论其他人)。

when using the install param with update-alternatives, you can set the priority in auto mode. Implicitly saying that the alternative with the highest priority will be the default alternative should no alternative have been set manually. using the above answer as anexample,

将安装参数与 update-alternatives 一起使用时,您可以在自动模式下设置优先级。暗示如果没有手动设置替代方案,则具有最高优先级的替代方案将是默认替代方案。以上面的答案为例,

update-alternatives --install /usr/bin/python python /usr/bin/python2 50

will set the python2 alternative with a priority of 50, and

将 python2 替代设置为 50 的优先级,和

update-alternatives --install /usr/bin/python python /usr/bin/python3.5 60

will set the python3.5 alternative with a priority of 60. and by default, the python 3.5 becomes the default python executable for the python command.

将优先级设置为 60. 的 python3.5 替代项。默认情况下,python 3.5 成为 python 命令的默认 python 可执行文件。

should you want to change your default python alternative,

如果您想更改默认的 Python 替代方案,

update-alternatives --config python

Find this a better approach as i don't have to modify my path files.

找到一种更好的方法,因为我不必修改我的路径文件。

回答by woodstock

I want to provide some additional context around why yum was broken in the OP, and why I think the alternativesmethod is the best approach. Perhaps there are other best practices, but I've made some discoveries and would like to share my findings.

我想提供一些额外的上下文来解释为什么 yum 在 OP 中被破坏,以及为什么我认为该alternatives方法是最好的方法。也许还有其他最佳实践,但我已经取得了一些发现,并想分享我的发现。

Assuming 3.5.2 was:

假设 3.5.2 是:

  1. installed separately (as suggested by OP) similar to the steps: Python Installation Procedure From Source
  2. the --prefix option for ./configure was updated from the default --prefix = /usr/local/bin to --prefix = /usr/bin/python3
  1. 单独安装(如 OP 所建议)类似于以下步骤:Python Installation Procedure From Source
  2. ./configure 的 --prefix 选项从默认的 --prefix = /usr/local/bin 更新为 --prefix = /usr/bin/python3

The command to link 'separately installed 3.5.2' at the location /usr/bin/python3 to system python at /usr/bin/python overwrote or otherwise modified system python, breaking yum.

将位于 /usr/bin/python3 位置的“单独安装的 3.5.2”链接到位于 /usr/bin/python 的系统 python 的命令覆盖或以其他方式修改了系统 python,从而破坏了 yum。

This approach complements @OldFart 's answer and hopefully provides some additional perspective around root cause of why a separately installed python can cause issues.

这种方法补充了@OldFart 的答案,并希望提供一些额外的视角来解释为什么单独安装的 python 会导致问题的根本原因。

update-alternativeswas a breath of fresh air for a similar problem I ran into

update-alternatives是我遇到的类似问题的新鲜空气