【问题标题】:Faulty python installation on macOS. How can I repair?macOS 上的 python 安装错误。我该如何修复?
【发布时间】:2018-05-20 21:04:56
【问题描述】:

我正在使用 macOS High Sierra。在过去的某个时候,我通过 brew 安装了 Python,但是由于某种原因(我不知道)pip 无法工作:

$ pip --version Traceback (most recent call last):
File "/usr/local/opt/python2/libexec/bin/pip", line 6, in <module>
  from pkg_resources import load_entry_point
File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 3195, in <module>
  @_call_aside
File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 3179, in _call_aside
  f(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 3208, in _initialize_master_working_set
  working_set = WorkingSet._build_master()
File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 683, in _build_master
  return cls._build_from_requirements(__requires__)
File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 696, in _build_from_requirements
  dists = ws.resolve(reqs, Environment())
File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 885, in resolve
  raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'pip==9.0.1' distribution was not found and is required by the application

因此我发出:

$ brew uninstall python@2
Uninstalling /usr/local/Cellar/python@2/2.7.14_1... (3,622 files, 50.6MB)

在这之后,我假设是 python 的默认安装

$ which python
/usr/bin/python

很遗憾,我无法通过键入 python 来运行 Python:

$ python --version
-bash: /usr/local/opt/python2/libexec/bin/python: No such file or directory

但是我可以通过直接引用它来运行那个 python 解释器:

$ /usr/bin/python --version
Python 2.7.10

python --version 不起作用,这让我很困扰,我想知道这是否是以前在此处安装了 brew 安装的 python 版本的结果。于是我决定用brew install python@2重新安装,成功了。

python的假设位置已经改变:

$ which python
/usr/local/bin/python

但是当我尝试使用它时仍然遇到同样的错误:

$ python --version
-bash: /usr/local/opt/python2/libexec/bin/python: No such file or directory

当然,使用 python 解释器的完整路径可以正常工作:

$ /usr/local/bin/python --version
Python 2.7.15

任何人都可以帮我诊断为什么即使 which python 声明 python 位于 /usr/local/bin/python,但简单地发出 python 会失败并显示 /usr/local/opt/python2/libexec/bin/python: No such file or directory?? p>

编辑:响应者要求运行file 的输出,这是输出:

$ file /usr/bin/python
/usr/bin/python: Mach-O universal binary with 2 architectures: [i386:Mach-O executable i386] [x86_64:Mach-O 64-bit executable x86_64]
/usr/bin/python (for architecture i386):    Mach-O executable i386
/usr/bin/python (for architecture x86_64):  Mach-O 64-bit executable x86_64
$ file /usr/local/bin/python
/usr/local/bin/python: Mach-O 64-bit executable x86_64

编辑:响应者要求输出brew doctor。这是输出:

Warning: Putting non-prefixed coreutils in your path can cause gmp builds to fail.

Warning: Unbrewed header files were found in /usr/local/include.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

Warning: Broken symlinks were found. Remove them with `brew prune`:
  /usr/local/bin/apm
  /usr/local/bin/atom

编辑:PATH 的输出,有趣的是 /usr/local/opt/python2/libexec/bin 在那里

$ echo $PATH
/usr/local/opt/coreutils/libexec/gnubin:/Applications/google-cloud-sdk/bin:/usr/local/opt/python2/libexec/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/share/dotnet:/Applications/Wireshark.app/Contents/MacOS
$ ls /usr/local/opt/python2/libexec/bin
ls: cannot access '/usr/local/opt/python2/libexec/bin': No such file or directory
$ ls /usr/local/opt/python2/libexec
pip  setuptools  wheel

【问题讨论】:

  • 请检查/usr/bin/pythonfile /usr/bin/python;输出是什么?
  • @AnttiHaapala 感谢您的回复。我用该信息更新了问题。
  • 好吧,我不会假装我知道这里发生了什么:D
  • 没关系。您提示我提供其他可能有用的信息。
  • brew doctor 的输出是什么?

标签: python macos homebrew


【解决方案1】:

解决方案可能只是从您的PATH 环境变量中删除/usr/local/opt/python2/libexec/bin

为此,您必须编辑您的 ~/.bash_profile 并删除该目录的出现。

【讨论】:

  • hmm...看看 .bash_profile 顶部的内容 :) $ head -1 /Users/jamiet/.bash_profile PATH="/usr/local/opt/python2/libexec/bin:$PATH"
  • 我已经删除了它,当启动一个新的 shell 时:$ python Python 2.7.15 (default, May 1 2018, 16:44:08) [GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.1)] on darwin Type "help", "copyright", "credits" or "license" for more information. &gt;&gt;&gt;
  • 格式化没有帮助,但基本上,它的工作原理:) 非常感谢。
猜你喜欢
  • 1970-01-01
  • 2020-01-15
  • 2021-04-10
  • 2015-08-03
  • 2018-12-04
  • 1970-01-01
  • 1970-01-01
  • 2018-10-23
  • 1970-01-01
相关资源
最近更新 更多