Faulty python installation on macOS. How can I repair?

强颜欢笑 提交于 2020-01-15 09:23:10

问题


I am using macOS High Sierra. In the past at some point I installed Python via brew however for some reason (that I don't know) pip failed to work:

$ 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

Hence I issued:

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

After which I was left with what I assume is the default installation of python

$ which python
/usr/bin/python

Unfortunately I can't run Python simply by typing python:

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

however I can run that python interpreter by referring to it directly:

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

It bothered me that python --version didn't work and I wondered if it was a consequence of previously having had a brew installed version of python on here. Hence I decided to reinstall using brew install python@2, which succeeded.

The supposed location of python has changed:

$ which python
/usr/local/bin/python

but I still get the same error when I try to use it:

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

Of course, using the full path to the python interpreter works fine:

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

Can anyone help me diagnose why simply issuing python fails with /usr/local/opt/python2/libexec/bin/python: No such file or directory even though which python states that python is located at /usr/local/bin/python?

Edit: A responder asked for the output of running file, here is that output:

$ 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

Edit: A responder asked for the output of brew doctor. Here is that output:

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

Edit: Output of PATH, interesting that /usr/local/opt/python2/libexec/bin is in there

$ 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

回答1:


The solution is probably simply to remove /usr/local/opt/python2/libexec/bin from your PATH environment variable.

To do this you have to edit your ~/.bash_profile and remove the occurence of this directory.



来源:https://stackoverflow.com/questions/50439358/faulty-python-installation-on-macos-how-can-i-repair

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!