简体   繁体   中英

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']

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM