77

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

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.

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).

Dolan Antenucci
  • 15,432
  • 17
  • 74
  • 100

4 Answers4

191
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).

wil3
  • 2,877
  • 2
  • 18
  • 22
  • 1
    Thanks, that solved the issue for me as well. I submitted a bug to the developer: https://github.com/dateutil/dateutil/issues/12 – Dolan Antenucci Dec 24 '14 at 13:05
  • 3
    I think updating your version of `six` would also work. – tacaswell Dec 24 '14 at 15:29
  • 3
    Thanks for your suggestion @tcaswell, I looked into it and I am already running the latest version of six. – wil3 Dec 24 '14 at 17:50
  • 1
    See https://bitbucket.org/gutworth/six/issue/39/sixmoves-missing-thread-_thread, maybe that is a year old though... – tacaswell Dec 28 '14 at 03:40
  • 1
    @tcaswell suggestion of updating six worked for me. I thought I was on the latest version (1.9.0) but it turns out that I had two versions installed, 1.9.0 and 1.4.0 -- finding the old one and deleting it worked great. Didn't try the dateutil solution. – Michael Scott Asato Cuthbert Jan 26 '15 at 15:36
  • Using OS X 10.10 (Yosemite) and the OS X system Python (2.7.6) -- I only have one version of `six` (1.9.0) and was still getting this error. The uninstall and reinstall of `python-dateutil` immediately fixed the problem. However, I am still having the (possibly related) problem of not being able to import `pyopencl` because of `from six.moves import intern`. So, all in all, awesome answer, this fixes the exact problem but just sharing that `six` seems still generally broken for OS X 10.10 and Python 2.7.6, FYI. Thanks again for this answer! –  Jan 28 '15 at 22:16
  • 1
    It worked for me in a python 2.7 installation on a Yosemite 10.10.3 machine. It seems though that quite a few python packages have issues with the "non-standard" locations of the Python libraries in MacOS X. – Costas B. Jun 23 '15 at 14:32
  • 2
    Seems to be a new issue in El Capitan -- an old version of "six" is included in ''/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six.py' which can't be upgraded without disabling rootless. – Michael Scott Asato Cuthbert Oct 01 '15 at 02:53
  • Removing the outdated six that comes with OSX python and pip installing a better version is the correct fix IMO, versus recommending downgrading a different package. – Marius Oct 30 '15 at 20:13
  • Instead of removing the system version. What I did is setting the system variable: PYTHONPATH='/Library/Python/2.7/site-packages'. Or in your application: import sys;sys.path.insert(1,'/Library/Python/2.7/site-packages') – Pablo Reyes Dec 11 '15 at 22:18
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.

Oriol Nieto
  • 5,409
  • 6
  • 32
  • 38
  • Unfortunately, this no longer works with El Capitan because of the protection of the rootless feature. – Michael Scott Asato Cuthbert Oct 01 '15 at 02:54
  • I just read what the "rootless feature" is (here http://apple.stackexchange.com/questions/193368/what-is-the-rootless-feature-in-el-capitan-really) and it seems like a terrible idea. I guess in El Capitan people will have to use a virtualenv in order to have full control of their Python installs. – Oriol Nieto Oct 02 '15 at 21:22
  • 2
    This worked for me on 10.10.4 (I also have a newer version of six which is installed by pip but not imported by default). Thanks. – Leo Fang Nov 30 '15 at 20:26
  • 1
    Instead of removing the system version. What I did is setting the system variable: PYTHONPATH='/Library/Python/2.7/site-packages'. Or in your application: import sys;sys.path.insert(1,'/Library/Python/2.7/site-packages') – Pablo Reyes Dec 11 '15 at 22:19
  • For some reason 'sudo pip install six -U' updated the six modules in my /usr/local/bin/python2.7/site-packages folder. I had to copy six.* from this folder to /Library/Python/2.7/site-packages to make it work. Was having error starting docker-compose ... – apil.tamang Oct 27 '16 at 12:25
10

Installing the python-dateutil==2.2 did not work for me.

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.

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.

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 :

$ pip install --ignore-installed six
$ pip install --ignore-installed ipython
Aziz Alto
  • 19,057
  • 5
  • 77
  • 60
10

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:

>>> 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:

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:

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)

Pablo Reyes
  • 3,073
  • 1
  • 20
  • 30