AttributeError: 'NoneType' object has no attribute 'magic'

Viewed 10626

I am getting error on the following python code. I'm running from a script. Im using python 3.6.2.

import matplotlib
matplotlib.use('Agg')

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from IPython import get_ipython

get_ipython().magic(u'matplotlib inline')

I have tried using both py and jpy extensions. Tried to change the syntax for importing matplotlib but nothing works.

The error from running with python shows.

Traceback (most recent call last):
  File "example01.py", line 11, in <module>
    get_ipython().magic(u'matplotlib inline')
AttributeError: 'NoneType' object has no attribute 'magic'

Error from running with iPython shows

/usr/local/lib/python3.6/site-packages/IPython/core/history.py:226: UserWarning: IPython History requires SQLite, your history will not be saved
  warn("IPython History requires SQLite, your history will not be saved")
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
~/python/week1/example01.py in <module>()
      9 from IPython import get_ipython
     10 
---> 11 get_ipython().magic(u'matplotlib inline')
     12 

/usr/local/lib/python3.6/site-packages/IPython/core/interactiveshell.py in magic(self, arg_s)
   2172         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2173         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2174         return self.run_line_magic(magic_name, magic_arg_s, _stack_depth=2)
   2175 
   2176     #-------------------------------------------------------------------------

/usr/local/lib/python3.6/site-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line, _stack_depth)
   2093                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2094             with self.builtin_trap:
-> 2095                 result = fn(*args,**kwargs)
   2096             return result
   2097 

<decorator-gen-107> in matplotlib(self, line)

/usr/local/lib/python3.6/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    185     # but it's overkill for just that one bit of state.
    186     def magic_deco(arg):
--> 187         call = lambda f, *a, **k: f(*a, **k)
    188 
    189         if callable(arg):

/usr/local/lib/python3.6/site-packages/IPython/core/magics/pylab.py in matplotlib(self, line)
     97             print("Available matplotlib backends: %s" % backends_list)
     98         else:
---> 99             gui, backend = self.shell.enable_matplotlib(args.gui)
    100             self._show_matplotlib_backend(args.gui, backend)
    101 

/usr/local/lib/python3.6/site-packages/IPython/core/interactiveshell.py in enable_matplotlib(self, gui)
   2976                 gui, backend = pt.find_gui_and_backend(self.pylab_gui_select)
   2977 
-> 2978         pt.activate_matplotlib(backend)
   2979         pt.configure_inline_support(self, backend)
   2980 

/usr/local/lib/python3.6/site-packages/IPython/core/pylabtools.py in activate_matplotlib(backend)
    306 
    307     import matplotlib.pyplot
--> 308     matplotlib.pyplot.switch_backend(backend)
    309 
    310     # This must be imported last in the matplotlib series, after

/usr/local/lib/python3.6/site-packages/matplotlib/pyplot.py in switch_backend(newbackend)
    231     matplotlib.use(newbackend, warn=False, force=True)
    232     from matplotlib.backends import pylab_setup
--> 233     _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
    234 
    235 

/usr/local/lib/python3.6/site-packages/matplotlib/backends/__init__.py in pylab_setup()
     30     # imports. 0 means only perform absolute imports.
     31     backend_mod = __import__(backend_name,
---> 32                              globals(),locals(),[backend_name],0)
     33 
     34     # Things we pull in from all backends

ModuleNotFoundError: No module named 'ipykernel'

After installing ipykernel trying to run the script using ipython gives me followng errors

/usr/local/lib/python3.6/site-packages/IPython/core/history.py:226: UserWarning: IPython History requires SQLite, your history will not be saved
  warn("IPython History requires SQLite, your history will not be saved")
---------------------------------------------------------------------------
UnknownBackend                            Traceback (most recent call last)
~/python/week1/example01.py in <module>()
      9 from IPython import get_ipython
     10 
---> 11 get_ipython().magic(u'matplotlib inline')
     12 

/usr/local/lib/python3.6/site-packages/IPython/core/interactiveshell.py in magic(self, arg_s)
   2172         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2173         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2174         return self.run_line_magic(magic_name, magic_arg_s, _stack_depth=2)
   2175 
   2176     #-------------------------------------------------------------------------

/usr/local/lib/python3.6/site-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line, _stack_depth)
   2093                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2094             with self.builtin_trap:
-> 2095                 result = fn(*args,**kwargs)
   2096             return result
   2097 

<decorator-gen-107> in matplotlib(self, line)

/usr/local/lib/python3.6/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    185     # but it's overkill for just that one bit of state.
    186     def magic_deco(arg):
--> 187         call = lambda f, *a, **k: f(*a, **k)
    188 
    189         if callable(arg):

/usr/local/lib/python3.6/site-packages/IPython/core/magics/pylab.py in matplotlib(self, line)
     97             print("Available matplotlib backends: %s" % backends_list)
     98         else:
---> 99             gui, backend = self.shell.enable_matplotlib(args.gui)
    100             self._show_matplotlib_backend(args.gui, backend)
    101 

/usr/local/lib/python3.6/site-packages/IPython/core/interactiveshell.py in enable_matplotlib(self, gui)
   2981         # Now we must activate the gui pylab wants to use, and fix %run to take
   2982         # plot updates into account
-> 2983         self.enable_gui(gui)
   2984         self.magics_manager.registry['ExecutionMagics'].default_runner = \
   2985             pt.mpl_runner(self.safe_execfile)

/usr/local/lib/python3.6/site-packages/IPython/terminal/interactiveshell.py in enable_gui(self, gui)
    499         if gui:
    500             self.active_eventloop, self._inputhook =\
--> 501                 get_inputhook_name_and_func(gui)
    502         else:
    503             self.active_eventloop = self._inputhook = None

/usr/local/lib/python3.6/site-packages/IPython/terminal/pt_inputhooks/__init__.py in get_inputhook_name_and_func(gui)
     36 
     37     if gui not in backends:
---> 38         raise UnknownBackend(gui)
     39 
     40     if gui in aliases:

UnknownBackend: No event loop integration for 'inline'. Supported event loops are: qt, qt4, qt5, gtk, gtk2, gtk3, tk, wx, pyglet, glut, osx
2 Answers

The inline backend is not supported in a terminal.

You may use Spyder and run this script from the IPython kernel in Spyder.

enter image description here

The matplotlib inline backend, which draws the matplotlib output as png images inside the output, requires an environment where images can actually be shown. Neither the python console, nor IPython console are able to do this.

Instead you need to use a Jupyter Qt console or a Jupyter notebook. Inside of those you can activate the inline backend using

%matplotlib inline

or

get_ipython().magic(u'matplotlib inline')

enter image description here

Related