macOS上安装有错误的python。我该怎么修?

时间:2018-05-20 21:04:57

标签: python macos homebrew

我正在使用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

任何人都可以帮助我诊断为什么只发出python失败 /usr/local/opt/python2/libexec/bin/python: No such file or directory ,即使 which python 指出python位于/usr/local/bin/python

编辑:响应者询问运行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

1 个答案:

答案 0 :(得分:1)

解决方案可能只是从PATH环境变量中删除~/.bash_profile

要执行此操作,您必须编辑df.toPandas().to_csv("/dbfs/<path>/df.csv", mode='w+', encoding='utf-8') server = smtplib.SMTP('smtp.gmail.com:587') server.ehlo() server.starttls() server.login("<email>", "<password>") sender = "<email>" recipient = "<email>" msg = MIMEMultipart() msg['Subject'] = 'I need help' msg['From'] = sender msg['To'] = recipient filedata = sc.textFile("/dbfs/<path>/df.csv", use_unicode=False) msg.attach(MIMEText('This is your test message with attachment...')) part = MIMEApplication("".join(filedata.collect()), Name="df.csv") part['Content-Disposition'] = 'attachment; filename="%s"' % 'df.csv' msg.attach(part) server.sendmail(sender, [recipient], msg.as_string()) server.close() 并删除此目录的出现。