无法 运行 Python 3 Homebrew 安装后

Unable to Run Python 3 After Homebrew Installation

在使用主页上的脚本安装 Homebrew 并使用 brew doctor 检查是否一切正常后,我发出 brew install python3 以便在我的 [=39= 上安装 Python 3 ].

一切似乎都很好,直到我尝试 运行 python3 --version;我最终得到:

-bash: /Library/Frameworks/Python.framework/Versions/3.5/bin/python3: No such file or directory

我检查了文件目录以查看发生了什么,实际上,我在我的框架文件夹中没有看到任何与 Python 有关的文件。看起来 Python 2.7 也不在我的 Mac 上。

这是我安装后得到的 Python 3:

Summary /usr/local/Cellar/python3/3.5.1: 3,438 files, 51.5M

edit_2:可能跟没有Python框架有关系?我刚从 Python 网站上读到这篇文章:

The Apple-provided build of Python is installed in /System/Library/Frameworks/Python.framework and /usr/bin/python, respectively. You should never modify or delete these, as they are Apple-controlled and are used by Apple- or third-party software. Remember that if you choose to install a newer Python version from python.org, you will have two different but functional Python installations on your computer, so it will be important that your paths and usages are consistent with what you want to do.

这个错误:

-bash: /Library/Frameworks/Python.framework/Versions/3.5/bin/python3: No such file or directory

建议使用不同的方式(不是 Homebrew)安装 Python 3 的一些先前(尝试)安装的残余。

(我认为这实际上是 www.python.org 的 Python 安装所在的位置。不过我不知道,因为我从未尝试过该软件包,但只安装了 www.python.org 版本。不过,这表明您已经尝试安装 Python 3.5,但失败了,您现在正在尝试 Homebrew。)

我建议将其移开(重命名),这样您的系统就不会接收它。像

mv /Library/Frameworks/Python.framework/Versions/3.5 /Library/Frameworks/Python.framework/Versions/3.5-aside

(如果该目录中还有 Python 3 的其他版本,您可能希望对这些版本执行相同的操作。)

同时检查 python3 不是别名。命令如

which python3
type python3
alias python3

会揭示这一点。

排除干扰 Python 3 后,再次尝试通过自制程序重新安装 Python 3。您可能需要卸载 + 重新安装。
安装完成后仔细阅读任何自制消息,特别是如果它提到有关链接文件的内容:您可能需要 运行 类似 brew link python3.

的内容

好的,这是我收集到的:

我遇到了同样的问题。我学会了如何永久修复它:

  1. 在 Mac Finder 中打开 "Applications" 并将 Python 拖到垃圾箱。
  2. 清空垃圾桶

如果您有上述错误,那么官方 Python 安装已通过例如执行(如其他人所述) Python.org。这会在 Bash 别名之外为 pythonpython3 命令创建某种别名。因此,虽然命令 where python3 可能指向 /usr/local/bin/python3,但 python3 仍会尝试调用 /Library/Frameworks/Python.framework/Versions/3.5/bin/python3.

注:

  • MacOS系统Python是/usr/bin/python
  • Homebrew Python(s) 将位于 /usr/local/bin/
  • Pythons 作为 Apple 应用程序安装在 /Library/Frameworks/Python.framework/

我想我发现了问题所在。

我猜想,在某个时刻,您是从官方网站而不是通过 Homebrew 安装的 python。 就我而言,我是通过官方网站 Python 3.6.4 安装的。几个月后,我想升级它,发现它非常复杂。所以,我决定转向 Homebrew。打开终端 window 让我们尝试解决这个问题:

  1. 首先,让我们卸载以前的 Python 版本:

     sudo rm -rf /Library/Frameworks/Python.framework
     sudo rm -rf /usr/local/bin/python3
    
  2. 然后,从$PATH变量中删除以前的框架:

     nano ~/.bash_profile
    

你会看到类似的东西:

    # Setting PATH for Python 2.7
    # The original version is saved in .bash_profile.pysave
    PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
    export PATH

    # Setting PATH for Python 3.6
    # The original version is saved in .bash_profile.pysave
    PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
    export PATH`

问题所在:这些路径不存在。评论 $PATH editions(或删除它们):

    # Setting PATH for Python 2.7
    # The original version is saved in .bash_profile.pysave
    # PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
    # export PATH

    # Setting PATH for Python 3.6
    # The original version is saved in .bash_profile.pysave
    # PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
    # export PATH
  1. 重启电脑,通过Homebrew安装Python 2和3:

     brew update
     brew install python
     brew install python3
    

这对我有用。现在,如果输入 python3 --version,我会得到 Python 3.7.0,一切正常 :)