在macOS上的python安装错误.我该如何修理? [英] Faulty python installation on macOS. How can I repair?

查看:88
本文介绍了在macOS上的python安装错误.我该如何修理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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 brew版本的结果.因此,我决定使用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时,发出 /usr/local/opt/python2/libexec/bin/python: No such file or directory 却简单地导致 /usr/local/opt/python2/libexec/bin/python: No such file or directory 失败了吗?

响应者要求运行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
 

解决方案

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

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

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

解决方案

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.

这篇关于在macOS上的python安装错误.我该如何修理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆