ubuntu python版本切换_在Ubuntu中更改Python3默认版本

2023-05-16

问题描述

我正在使用Ubuntu 16.04 LTS。我安装了python3。安装了两个版本,python 3.4.3和python 3.6。每当我使用python3命令时,默认情况下它将使用python 3.4.3。我想将python 3.6与python3一起使用。

python3 --version显示为version 3.4.3

我正在安装支持version > 3.5的ansible。因此,每当我在终端中键入ansible时,由于python 3.4都会引发错误

最佳办法

从评论:

sudo update-alternatives --config python

会显示一个错误:

update-alternatives: error: no alternatives for python3

您需要更新update-alternatives,然后才能设置默认的python版本。

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.4 1

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 2

然后运行:

sudo update-alternatives --config python

将python3.6设置为默认值。

或使用以下命令将python3.6设置为默认值:

sudo update-alternatives --set python /usr/bin/python3.6

次佳办法

您可以通过以下简单的步骤实现此目标-

在终端上检查python版本-python --version

获取root用户特权。在端子上-sudo su

写下root用户密码

执行此命令以切换到python 3.6-update-alternatives --install /usr/bin/python python /usr/bin/python3 1

检查python版本-python --version

做完了

第三种办法

为/usr /bin /python3创建符号链接。在我的LinuxMint中:

# ls -lh /usr/bin/python3 /usr/bin/python

lrwxrwxrwx 1 root root 9 ноя 24 2017 /usr/bin/python -> python2.7

lrwxrwxrwx 1 root root 9 сен 6 2017 /usr/bin/python3 -> python3.5

# mv /usr/bin/python

# cp /usr/bin/python3 /usr/bin/python

# python --version

Python 3.5.2

第四种办法

使用这些命令可以帮助您:

检查python的版本:ls /usr/bin/python*

别名:alias python='/usr/bin/pythonxx'

re-login:. ~/.bashrc

再次检查python版本:python --version

第五种办法

一个简单的答案是为python3.6添加一个别名。

只需将这一行添加到文件〜/.bashrc中:alias python3="python3.6",然后关闭您的终端并打开一个新终端。现在,当您键入python3 xxx时,它将转换为python3.6 xxx。

此解决方案可解决您的问题,而无需过多地调整系统。

编辑:

正如Mikael Kjær指出的那样,这是对您的系统的错误配置。

Set the ansible_python_interpreter configuration option to /usr/bin/python3. The ansible_python_interpreter configuration option is usually set per-host as an inventory variable associated with a host or group of hosts:

# Example inventory that makes an alias for localhost that uses python3 [py3-hosts] localhost-py3 ansible_host=localhost ansible_connection=local [py3-hosts:vars] ansible_python_interpreter=/usr/bin/python3

如关于配置文件的here所示:

Changes can be made and used in a configuration file which will be processed in the following order:

* ANSIBLE_CONFIG (an environment variable) * ansible.cfg (in the current directory) * .ansible.cfg (in the home directory) * /etc/ansible/ansible.cfg

参考资料

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ubuntu python版本切换_在Ubuntu中更改Python3默认版本 的相关文章

随机推荐