Matplotlib issue in OS X ("ImportError: cannot import name _thread")

At some point in the last few days, Matplotlib stopped working for me on OS X. Here is 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 

The only system change I can think of is Appleโ€™s NTP update, and possibly some of the rights changes I made in / usr / local to work with Brew again.

I tried reinstalling both Matplotlib and Python-dateutil via Pip, but that didn't help. Also tried rebooting. I am running Python 2.7.6, which is located in / usr / bin / python. I am running Yosemite (OS X 10.10.1).

+74
python matplotlib python-dateutil macos
Dec 24 '14 at 0:51
source share
4 answers
 sudo pip uninstall python-dateutil sudo pip install python-dateutil==2.2 

I also had an error message this afternoon, although I recently upgraded to Yosemite. I'm not quite sure that I understand why returning dateutil to the previous version works for me, but since I am doing the above, I have no problems (usually I use pyplot inline in ipython laptop).

+186
Dec 24 '14 at 9:06
source share

This issue has been fixed in the latest versions of six and dateutil . However, in OS X, even if you upgrade your six to the latest version, you may not be able to update it correctly. Here's what happened to me:

After running pip2 install six -U new six module was installed in /Library/Python/2.7/site-packages/ . However, when I downloaded six to the 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 used 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.

+43
Jun 30 '15 at 13:50
source share

You may have a fully installed version of any packages that you installed, but the default version is not the one you need. You can view the list of python search paths to find its packages as follows:

 >>> import sys >>> sys.path 

To let python first search for the most updated version of a specific package, instead of removing the system version, what you can do is set the PYTHONPATH system variable to ~ / .bash_profile (or ~ / .bashrc if linux) in the path where the new packages are installed :

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

An alternative is to change the python path inside your python script by adding the path at the beginning of the path list:

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

This needs to be done for each script you need a specific version of the package. For some reason, you might want to use the old version that you installed. BTW all my installations using easy_install, or pip, or from sources go to / Library / Python / 2.7 / site-packages This worked in EL Capitan, and now also in macOS Sierra (10.12.2)

+10
Dec 11 '15 at 22:24
source share

Setting python-dateutil==2.2 did not help me.

But a quick and dirty workaround really worked! I am replacing six.py with python 2.7 using six.py from python 3.4 (virtualenv). Since my problem is 2.7, but not 3.4.

UPDATE

I had the same problem again after reinstalling python (and after upgrading to El Capitan). The strange thing is that this error only occurs in the IPython shell and laptop (when I do import matplotlib.pyplot as plt ), but it works fine with the Python shell.

So the best solution (working in my case) without the dirty work is to install both six and IPython . Here is what I did to fix this:

 $ pip install --ignore-installed six $ pip install --ignore-installed ipython 
+9
Apr 13 '15 at 19:49
source share



All Articles