OS X上的Matplotlib问题(" ImportError:无法导入名称_thread")

时间:2014-12-24 00:51:32

标签: python macos python-2.7 matplotlib python-dateutil

在最近几天的某个时刻,Matplotlib在OS X上停止为我工作。这是我在尝试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

我能想到的唯一系统变化是Apple强制的NTP更新,也许我在/ usr / local做了一些权限更改,让Brew再次运行。

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

4 个答案:

答案 0 :(得分:191)

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

我今天下午也有同样的错误信息,虽然我最近升级到优胜美地。我不完全确定我理解为什么将dateutil恢复到以前的版本对我有用,但是因为运行上面的我没有遇到麻烦(我通常在ipython笔记本中使用pyplot内联)。

答案 1 :(得分:45)

This problem is fixed in the latest six and dateutil versions. However, in OS X, even if you update your six to the latest version, you might not actually update it correctly. This is what happened to me:

After doing a pip2 install six -U, the new six module was installed in /Library/Python/2.7/site-packages/. However, when I loaded six in a python 2.7 terminal, and checked its path, this is what I got:

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

So, python was using an old version of six, which I removed by typing:

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

This fixed this issue for me.

答案 2 :(得分:10)

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

但是快速而肮脏的解决方法确实有效!我用python 3.4(virtualenv)中的six.py替换python 2.7中的six.py。因为,我在2.7中遇到问题,但不是3.4。

<强>更新

重新安装python后(以及升级到El Capitan之后)我又遇到了同样的问题。不明显的是,这个错误只发生在IPython shell和笔记本中(当我做import matplotlib.pyplot as plt时),但是在Python shell中工作正常。

所以更好的解决方案(在我的情况下确实有效)没有肮脏的解决方法是强制安装sixipython。以下是我修复此问题的方法:

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

答案 3 :(得分:10)

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

>>> import sys
>>> sys.path

为了让python首先搜索某个包的最新版本,而不是删除系统版本,可以做的是在〜/ .bash_profile中设置系统变量PYTHONPATH(或〜/ .bashrc if linux)将文件配置到安装新软件包的路径:

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)