简体   繁体   English

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

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

Is there a way to set the Python 3.5.2 as the default Python version on CentOS 7?有没有办法将 Python 3.5.2 设置为 CentOS 7 上的默认 Python 版本? currently, I have Python 2.7 installed as default and Python 3.5.2 installed separately.目前,我默认安装了 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 yum gives 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 版本?

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/.bashrc with 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 page to get started.阅读此页面以开始使用。

I would suggest using alternatives instead.我建议改用替代品

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 Python versions, the alternatives command, it's concepts and uses remain the same regardless of version numbers.尽管这个答案是指/使用特定的Python版本, alternatives命令,但无论版本号如何,它的概念和用途都保持不变。 It is strongly suggested that you read/learn more about the alternatives command 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 alternatives command .这是一个非常好的和简单的替代命令解释的链接

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. 将安装参数与 update-alternatives 一起使用时,您可以在自动模式下设置优先级。 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 an example, 以上面的答案为例,

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.找到一种更好的方法,因为我不必修改我的路径文件。

Option 1) Creating a soft link actually has a drawback.选项 1) 创建软链接实际上有一个缺点。 "yum" does not support Python3. “yum”不支持 Python3。 so, if you still decide to go with symlink creation then you also need to update the /usr/bin/yum因此,如果您仍然决定使用符号链接创建,那么您还需要更新 /usr/bin/yum

ln -s /usr/bin/python3 /usr/bin/python

And update the shebang line with #!/usr/bin/python2 in /usr/bin/yum file并在 /usr/bin/yum 文件中使用 #!/usr/bin/python2 更新 shebang 行

Option 2) use alternatives选项 2) 使用替代品

alternatives --install /usr/bin/python python /usr/bin/python3.x 60
alternatives --config python 

Option 3) create an alias in bash_profile选项 3) 在 bash_profile 中创建别名

alias python="/usr/bin/python3"

I want to provide some additional context around why yum was broken in the OP, and why I think the alternatives method is the best approach.我想提供一些额外的上下文来解释为什么 yum 在 OP 中被破坏,以及为什么我认为alternatives方法是最好的方法。 Perhaps there are other best practices, but I've made some discoveries and would like to share my findings.也许还有其他最佳实践,但我已经取得了一些发现,并想分享我的发现。

Assuming 3.5.2 was:假设 3.5.2 是:

  1. installed separately (as suggested by OP) similar to the steps: Python Installation Procedure From Source单独安装(如 OP 所建议)类似于以下步骤: 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 ./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-alternatives was a breath of fresh air for a similar problem I ran into update-alternatives是我遇到的类似问题的新鲜空气

This is what i am getting after setting the alternatives config option for python 这是在为python设置替代配置选项后我得到的

[root@localhost ~]# update-alternatives --config python

There are 2 programs which provide 'python'.

  Selection    Command
-----------------------------------------------
   1           /usr/bin/python2.7
*+ 2           /usr/bin/python3.7

Enter to keep the current selection[+], or type selection number: 2
[root@localhost ~]#
[root@localhost ~]# python
-bash: /usr/bin/python: No such file or directory

Using alternatives to set default to python3.6 still breaks yum.使用alternatives将默认值设置为 python3.6 仍然会破坏 yum。 To fix change the python interpreter to /usr/bin/python2.7 in two files.要修复,请在两个文件中将 python 解释器更改为/usr/bin/python2.7

[root@centos7 ~]# cat /usr/bin/yum
#!/usr/bin/python2.7
[root@centos7 ~]# cat /usr/libexec/urlgrabber-ext-down
#!/usr/bin/python2.7

I found the following article to be useful: https://rakeshjain-devops.medium.com/how-to-install-python-3-on-centos-7-7-using-yum-and-source-and-set-as-default-1dee13396f7我发现以下文章很有用: https://rakeshjain-devops.medium.com/how-to-install-python-3-on-centos-7-7-using-yum-and-source-and-set -as-default-1dee13396f7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM