Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failure to import matplotlib.pyplot in jupyter (but not ipython)

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']
like image 361
DeusXMachina Avatar asked Aug 08 '16 21:08

DeusXMachina


People also ask

Why is matplotlib not importing?

The Python "ModuleNotFoundError: No module named 'matplotlib'" occurs when we forget to install the matplotlib module before importing it or install it in an incorrect environment. To solve the error, install the module by running the pip install matplotlib command.

How do I import matplotlib into Jupyter notebook?

Make sure you first have Jupyter notebook installed, then we can add Matplotlib to our virtual environment. To do so, navigate to the command prompt and type pip install matplotlib. Now launch your Jupyter notebook by simply typing jupyter notebook at the command prompt.

Can Jupyter run matplotlib?

IPython kernel of Jupyter notebook is able to display plots of code in input cells. It works seamlessly with matplotlib library.


2 Answers

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.

like image 55
user6694492 Avatar answered Oct 13 '22 18:10

user6694492


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.

like image 44
DeusXMachina Avatar answered Oct 13 '22 18:10

DeusXMachina