【问题标题】:Python 2.7.5 and Python 3.6.5Python 2.7.5 和 Python 3.6.5
【发布时间】:2018-06-24 05:36:15
【问题描述】:

我已经安装了 Python 3.6.5,但是当我输入 Python 时,它显示 Python 2.7.5。 我想使用 Python 3。

[aravind@aravind05 Python-3.6.5]$ python3 --version
Python 3.6.5

[aravind@aravind05 Python-3.6.5]$ python
Python 2.7.5 (default, Apr 11 2018, 07:36:10)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

我想使用 Python 3

【问题讨论】:

  • 可以选择卸载 Python 2.7.5 吗?
  • 不要改变它!将 python 设置为 Python 3 可能会破坏您的系统工具。请改用python3 或虚拟环境。

标签: python python-3.x


【解决方案1】:

有多种方法可以实现这一点。

假设您在某些 *NIX 操作系统上使用类似 bourne-shell 的 shell,您可以定义一个别名 alias python python3

您还可以将一个名为“python”的符号链接,指向 python3 放置到前面在 PATH 中列出的目录中:

# on debian-like distributions, the following directories should be reasonable
ln -s ~/.local/bin/python "$(which python3)"
# place the following line into your .bashrc
export PATH="~/.local/bin/python:$PATH"

【讨论】:

  • @AfloroaieRobert 这不是一个很好的答案。别名仅影响当前 shell,不影响任何其他(系统)命令。
  • @melpomene,我已经阅读了别名的手册页,你是对的。感谢您的观察。
  • [aravind@aravind05 /]$ alias python python3 bash: alias: python: not found bash: alias: python3: not found
  • 亲爱的,我的 wifi 现在不工作了,到昨天还可以正常工作。
  • [aravind@aravind05 ~]$ nmcli 一般状态 STATE CONNECTIVITY WIFI-HW WIFI WWAN-HW WWAN 已连接完全启用启用启用禁用
【解决方案2】:

就像 Klaus 在评论中所说,更改系统 Python 版本可能会破坏系统内容。

改为使用 virtualenv。如果您需要帮助,Here 是一个很好的解释。

【讨论】:

    【解决方案3】:

    由于您的系统中安装了两个版本的 Python,因此您需要将默认 Python 定向到 Python 3.x。您可以通过在 .bashrc file 中添加几行来做到这一点。

    打开你的 .bashrc 文件 nano ~/.bashrc。在文件顶部的新行中键入 alias python=python3 然后使用 ctrl+o 保存文件并使用 ctrl+x 关闭文件。然后,回到你的命令行类型 source ~/.bashrc。现在你的别名应该是永久的了。

    编辑:

    对于更新选项,优先级是一个整数。优先级表示应该首先使用哪个程序。这篇文章总结得很好。

    这里是相关问题:https://stackoverflow.com/a/41986843/4982185

    【讨论】:

      猜你喜欢
      • 2018-11-21
      • 1970-01-01
      • 1970-01-01
      • 2013-07-04
      • 1970-01-01
      • 1970-01-01
      • 2014-01-19
      • 2020-12-17
      • 1970-01-01
      相关资源
      最近更新 更多