在OS X上出现的Matplotlib问题(“ImportError:无法导入名称_thread”)

77
在过去几天中,Matplotlib 在我的 OS X 上停止工作。在尝试导入 matplotlib 时,我遇到了以下错误:

At some point in the last few days, Matplotlib stopped working for me on OS X. Here's the error I get when trying to import matplotlib:


Traceback (most recent call last):
  File "/my/path/to/script/my_script.py", line 15, in <module>
    import matplotlib.pyplot as plt
  File "/Library/Python/2.7/site-packages/matplotlib/pyplot.py", line 34, in <module>
    from matplotlib.figure import Figure, figaspect
  File "/Library/Python/2.7/site-packages/matplotlib/figure.py", line 40, in <module>
    from matplotlib.axes import Axes, SubplotBase, subplot_class_factory
  File "/Library/Python/2.7/site-packages/matplotlib/axes/__init__.py", line 4, in <module>
    from ._subplots import *
  File "/Library/Python/2.7/site-packages/matplotlib/axes/_subplots.py", line 10, in <module>
    from matplotlib.axes._axes import Axes
  File "/Library/Python/2.7/site-packages/matplotlib/axes/_axes.py", line 22, in <module>
    import matplotlib.dates as _  # <-registers a date unit converter
  File "/Library/Python/2.7/site-packages/matplotlib/dates.py", line 126, in <module>
    from dateutil.rrule import (rrule, MO, TU, WE, TH, FR, SA, SU, YEARLY,
  File "/Library/Python/2.7/site-packages/dateutil/rrule.py", line 14, in <module>
    from six.moves import _thread
ImportError: cannot import name _thread

我能想到的唯一系统更改是苹果强制的NTP更新,以及我在/usr/local进行的一些权限更改,以使Brew再次工作。

我尝试通过Pip重新安装Matplotlib和Python-dateutil,但这没有帮助。也尝试了重启。我正在运行Python 2.7.6,它位于/usr/bin/python中。我正在运行Yosemite(OS X 10.10.1)。

4个回答

191
sudo pip uninstall python-dateutil
sudo pip install python-dateutil==2.2

今天下午我也遇到了相同的错误信息,尽管我最近升级到了Yosemite。我不太确定为什么将dateutil回滚到以前的版本对我有效,但自从运行上述操作后,我没有遇到任何问题(通常我在ipython笔记本中使用pyplot inline)。


3
我认为更新你的 six 版本也可以解决问题。 - tacaswell
3
感谢你的建议@tcaswell,我已经查看并使用最新版本的six。 - wil3
1
请参见 https://bitbucket.org/gutworth/six/issue/39/sixmoves-missing-thread-_thread,尽管可能是一年前的内容... - tacaswell
1
它在我的Yosemite 10.10.3机器上的Python 2.7安装中运行良好。然而,似乎相当多的Python包在MacOS X中的“非标准”Python库位置存在问题。 - Costas B.
2
在El Capitan中似乎出现了一个新问题——“/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six.py”中包含了一个旧版本的“six”,如果不禁用rootless,则无法升级。 - Michael Scott Asato Cuthbert
显示剩余5条评论

45

这个问题在最新的sixdateutil版本中已经解决。然而,在OS X上,即使你将six更新到最新版本,可能并没有正确地更新。这就是我遇到的情况:

执行pip2 install six -U后,新的six模块被安装在/Library/Python/2.7/site-packages/中。然而,当我在 Python 2.7 终端中加载 six 并检查其路径时,得到的结果是:

import six
print six.__file__
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six.pyc

所以,Python使用了一个旧版本的six,我通过键入以下命令将其删除:

所以,python使用了一个旧版本的six,我通过键入以下命令将其删除:

rm -rf /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six.*

这个方法解决了我的问题。


不幸的是,由于根保护功能的保护,这种方法在El Capitan上已经失效了。 - Michael Scott Asato Cuthbert
我刚刚读了关于“无根特性”是什么的解释(在这里http://apple.stackexchange.com/questions/193368/what-is-the-rootless-feature-in-el-capitan-really),看起来这是个糟糕的想法。我猜在El Capitan中,人们将不得不使用virtualenv来完全控制他们的Python安装。 - Oriol Nieto
2
这对我在10.10.4上起作用(我还安装了pip安装的更新版本的six,但默认情况下未导入)。谢谢。 - Leo Fang
1
不是删除系统版本,我所做的是设置系统变量:PYTHONPATH='/Library/Python/2.7/site-packages'。或者在您的应用程序中执行以下操作:import sys;sys.path.insert(1,'/Library/Python/2.7/site-packages') - Pablo Reyes
由于某种原因,'sudo pip install six -U' 更新了我 /usr/local/bin/python2.7/site-packages 文件夹中的 six 模块。我不得不将 six.* 从此文件夹复制到 /Library/Python/2.7/site-packages 以使其正常工作。之前启动 docker-compose 时一直出现错误... - apil.tamang

10

可能您已经完美安装了任何已安装的软件包的版本,但默认使用的版本不是您想要的版本。您可以按以下方式查看Python搜索以找到其软件包的路径列表:

>>> import sys
>>> sys.path

为了让Python首先搜索特定软件包的最新版本,而不是删除系统版本,可以在~/.bash_profile(或Linux上的~/.bashrc)配置文件中设置系统变量PYTHONPATH为新软件包安装的路径:

export PYTHONPATH=/Library/Python/2.7/site-packages

另一种方法是通过在路径列表的开头添加路径来修改 Python 脚本中的 Python 路径:

import sys
sys.path.insert(1,'/Library/Python/2.7/site-packages')

每个脚本都需要执行此操作,以使用所需的特定软件包版本。您可能因某种原因想要使用已安装的旧版本。 顺便说一下,我所有使用easy_install、pip或源码安装的软件都会安装在/Library/Python/2.7/site-packages中。 这在El Capitan上可行,现在也适用于macOS Sierra(10.12.2)。


10

安装 python-dateutil==2.2 对我没有起作用。

但是有一个快速而不太正式的解决方法可以解决问题!我在 Python 2.7 中使用来自 Python 3.4(virtualenv)的 six.py 替换了原本的 six.py。既然问题只出现在 2.7 中,而在 3.4 中却没有出现。

更新

重新安装 Python 后(并升级到 El Capitan 后),我再次遇到了同样的问题。不明显的是,这个错误只会在 IPython shell 和 notebook 中发生(当我执行 import matplotlib.pyplot as plt 时),但是从 Python shell 中可以正常工作。

因此,一个更好的解决方案(在我的情况下有效)是强制安装 sixipython。以下是我完成此操作的步骤:

$ pip install --ignore-installed six
$ pip install --ignore-installed ipython

1
我有相同的问题。dateutil 的修复没有起作用。我不太想对我的2.7进行黑客攻击,还有更好的想法吗? - user1460739

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接