8

Update: ipykeynel 4.4.1 patched this issue the morning of Aug 9.

I have a fresh install and I have been trying to get my python dependencies up and running, namely jupyter notebook and matplotlib. I've pip installed everything, and "import matplotlib" works. If I am in a jupyter notebook, and I try "import matplotlib.pyplot" or "from matplotlib import pyplot as plt", I get:

ImportError                               Traceback (most recent call last)
...
/usr/local/lib/python2.7/dist-packages/IPython/core/pylabtools.pyc in configure_inline_support(shell, backend)
    359     except ImportError:
    360         return
--> 361     from matplotlib import pyplot
    362
    363     cfg = InlineBackend.instance(parent=shell)

ImportError: cannot import name pyplot

Full traceback

However, if I am in ipython (command line), this works fine. Also, running plots from a module from the command line, fine. I have tried a variety of techniques:

  • Pip install / uninstall matplotlib, ipython, and jupyter in various order
  • Using pip with --no-cache-dir and/or --ignore-installed
  • Deleting ~/.cache, ~/.ipython and ~/.jupyter
  • Making sure no packages are installed with apt-get, only installed with pip
  • Using apt-get to install python-matplotlib, ipython, and python-jupyter

It feels like I have mangled some sort of path information, but I cannot locate what or where would cause this, especially after multiple pip uninstall/reinstall and cache clearing. I've read every SO question relating to importing matplotlib, none have been helpful.

I rolled back to matplotlib 1.4.3, and that worked, but it lacks a couple of features I need. I realize this is probably a tricky one, so if you have any insight, even if incomplete, that would be greatly appreciated. Also, if this is something worthy of a bug report (never done one, not sure if this is a matplotlib problem, or just locally goofed up), comment as such and I'll submit one. Thanks!

System info:

Linux Mint 18 "Sarah"
Python==2.7.12
ipykernel==4.4.0
ipython==5.0.0
ipython-genutils==0.1.0
ipywidgets==5.2.2
jupyter==1.0.0
jupyter-client==4.3.0
jupyter-console==5.0.0
jupyter-core==4.1.0
notebook==4.2.2
numpy==1.11.1
pip 8.1.2 from /usr/local/lib/python2.7/dist-packages (python 2.7)

Output of sys.path in ipython and jupyter (same for both):

['',
 '/usr/local/bin',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PILcompat',
 '/usr/lib/python2.7/dist-packages/gtk-2.0',
 '/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
 '/usr/local/lib/python2.7/dist-packages/IPython/extensions',
 '/home/mm/.ipython']
DeusXMachina
  • 1,239
  • 1
  • 18
  • 26
  • 3
    Funny, ran into this issue this morning, your question is the only directly relevant Google search result. It seems that under some combination of package versions, initializing the "inline" backend leads to a circular import of `matplotlib.pyplot`. This is why everything is fine in Python and IPython shells (they use the default system backend). – Igor Raush Aug 08 '16 at 23:55
  • @IgorRaush I just learned about the backend features yesterday, after getting fed up and switching to Ipy3 (which only required changing 5 lines in my library, yay __future__!) and discovering it defaults to inline. I like the interactive zoom much of the time. Trying to get Qt5 to work is another bucket of _fun_. Python pro tip: check the backend in Ipy with `matplotlib.get_backend()` – DeusXMachina Aug 09 '16 at 14:40

2 Answers2

6

I have the same problem, and the problem maybe produced by ipykernel. after i roll back ipykernel version to 4.3.1. the problem solved.

just like @Igor Raush said, it's look like import circular of matplotlib.pyplot.

1

As mentioned here, using the magic line %matplotlib allows me to use the plot-in-new-window backend (Qt4Agg in my case). I did not know you could use %matplotlib by itself, without an argument. Even though an update to ipykernel 4.4.1 fixes this issue, I thought the magic line trick was pretty clever, and may clear up other import weirdness/bugs in the future.

Community
  • 1
  • 1
DeusXMachina
  • 1,239
  • 1
  • 18
  • 26