Python OS X 上的 Matplotlib 问题(“导入错误:无法导入名称 _thread”)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27630114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Matplotlib issue on OS X ("ImportError: cannot import name _thread")
提问by Dolan Antenucci
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. Also tried a reboot. I'm running Python 2.7.6, which is located in /usr/bin/python. I'm running Yosemite (OS X 10.10.1).
我尝试通过 Pip 重新安装 Matplotlib 和 Python-dateutil,但这没有帮助。也试过重启。我正在运行 Python 2.7.6,它位于 /usr/bin/python 中。我正在运行优胜美地(OS X 10.10.1)。
采纳答案by wil3
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).
今天下午我也收到了同样的错误消息,尽管我最近升级到了 Yosemite。我不完全确定我明白为什么将 dateutil 恢复到以前的版本对我有用,但是自从运行上面的我没有问题(我通常在 ipython 笔记本中使用 pyplot inline)。
回答by Aziz Alto
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). Since, I have the problem in 2.7 but not 3.4.
但是一个快速而肮脏的解决方法确实奏效了!我用python 3.4 (virtualenv)替换six.py
了 python 2.7 six.py
。因为,我在 2.7 中遇到了问题,但在 3.4 中没有。
UPDATE
更新
I had the same problem again after reinstalling python (and after upgrading to 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.
重新安装 python 后(以及升级到 El Capitan 后),我再次遇到了同样的问题。不明显的是,此错误仅发生在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
. Here is what I did to have this fixed :
所以一个更好的解决方案(在我的情况下确实有效)没有肮脏的解决方法是强制安装six
和ipython
. 这是我为解决此问题所做的工作:
$ pip install --ignore-installed six
$ pip install --ignore-installed ipython
回答by Oriol Nieto
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:
此问题已在最新版本six
和dateutil
版本中修复。但是,在 OS X 中,即使您更新six
到最新版本,您实际上也可能无法正确更新它。这就是发生在我身上的事情:
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:
完成后pip2 install six -U
,新six
模块安装在/Library/Python/2.7/site-packages/
. 但是,当我加载six
python 2.7 终端并检查其路径时,这就是我得到的:
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.
这为我解决了这个问题。
回答by Pablo Reyes
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首先搜索某个包的最新版本,而不是删除系统版本,可以做的是PYTHONPATH
将~/.bash_profile(或~/.bashrc,如果是linux)配置文件中的系统变量设置为安装新包的路径:
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) 中