简体   繁体   English

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

[英]Matplotlib issue on OS X (“ImportError: cannot import name _thread”)

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 : 在最近几天的某个时候,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

The only system change I can think of was the Apple-forced NTP update and maybe some permission changes I did in /usr/local to get Brew working again. 我能想到的唯一的系统更改是Apple强制的NTP更新,也许我在/ usr / local中进行了一些权限更改,以使Brew重新工作。

I tried reinstalling both Matplotlib and Python-dateutil via Pip, but this did not help. 我尝试通过Pip重新安装Matplotlib和Python-dateutil,但这没有帮助。 Also tried a reboot. 还尝试了重启。 I'm running Python 2.7.6, which is located in /usr/bin/python. 我正在运行/ usr / bin / python中的Python 2.7.6。 I'm running Yosemite (OS X 10.10.1). 我正在运行优胜美地(OS X 10.10.1)。

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

I had the same error message this afternoon as well, although I did recently upgrade to Yosemite. 尽管我最近升级到优胜美地,但今天下午我也收到了同样的错误消息。 I'm not totally sure I understand why reverting dateutil to a previous version works for me, but since running the above I'm having no trouble (I generally use pyplot inline in an ipython notebook). 我不太确定我是否理解为什么将dateutil还原到以前的版本对我有用,但是由于运行上述命令,我没有遇到任何麻烦(我通常在ipython笔记本中使用pyplot inline)。

This problem is fixed in the latest six and dateutil versions. 最新的sixdateutil版本中已解决此问题。 However, in OS X, even if you update your six to the latest version, you might not actually update it correctly. 但是,在OS X中,即使将six更新为最新版本,也可能实际上未正确更新。 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/ . 在完成pip2 install six -U ,新的six模块已安装在/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: 但是,当我在python 2.7终端中加载six并检查其路径时,这就是我得到的:

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: 因此,python使用的是旧版本的six ,我通过键入以下内容将其删除:

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

This fixed this issue for me. 这为我解决了这个问题。

Installing the python-dateutil==2.2 did not work for me. 安装python-dateutil==2.2对我不起作用。

But a quick-and-dirty workaround did work! 但是,一种快速而又肮脏的解决方法确实有效! I replace six.py in python 2.7 with the six.py from python 3.4 (virtualenv). 我更换six.py在Python 2.7与six.py蟒蛇3.4(virtualenv中)。 Since, I have the problem in 2.7 but not 3.4. 从那以后,我在2.7中遇到了问题,但没有3.4。

UPDATE 更新

I had the same problem again after reinstalling python (and after upgrading to El Capitan). 重新安装python之后(升级到El Capitan之后),我又遇到了同样的问题。 Un-obvious thing is that this error occurs only in the IPython shell and notebook (when I do import matplotlib.pyplot as plt ) but works fine from a Python shell. 不明显的是,此错误仅在IPython Shell和笔记本中发生(当我import matplotlib.pyplot as plt ),但在Python Shell中可以正常工作。

So a better solution (that did work in my case) without a dirty work-around is to force install both six and ipython . 因此,一个更好的解决方案(在我的情况下确实有效)且没有不可行的解决方法是强制同时安装sixipython Here is what I did to have this fixed : 这是我修复此问题的方法:

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

It is possible that you have a perfectly installed version of any packages you have installed, but the version used by default is not the one you want. 您可能已经完美安装了所有已安装软件包的版本,但是默认情况下使用的不是您想要的版本。 You can see the list of paths that python search from in order to find its packages as follows: 您可以查看python搜索的路径列表,以查找其包,如下所示:

>>> import sys
>>> sys.path

In order to let python search first the most updated version of certain package, instead of removing the system version, what can be done is to set the system variable PYTHONPATH in the ~/.bash_profile (or ~/.bashrc if linux) config file to the path where the new packages are installed: 为了让python首先搜索某些软件包的最新版本,而不是删除系统版本,可以做的是在〜/ .bash_profile(如果是linux,则为〜/ .bashrc)配置文件中设置系统变量PYTHONPATH 。到安装新软件包的路径:

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

An alternative is to modify the python path inside your python script by adding the path at the beginning of the path list: 一种替代方法是通过在路径列表的开头添加路径来修改python脚本中的python路径:

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

This needs to be done for every script you need a certain package version. 对于需要特定软件包版本的每个脚本,都需要这样做。 You might want for some reason use an older version that you have installed. 您可能出于某种原因想要使用已安装的旧版本。 BTW all my installations with easy_install, or pip, or from sources go to /Library/Python/2.7/site-packages This worked en EL Capitan, and now also in macOS Sierra (10.12.2) 顺便说一句,我使用easy_install或pip进行的所有安装,或从源代码进行的安装,都转到/Library/Python/2.7/site-packages。此方法在EL Capitan上有效,现在在macOS Sierra(10.12.2)中也有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM