title: AttributeError: 'NoneType' object has no attribute 'magic'
body: <p>I am getting error on the following python code. I'm running from a script. Im using python 3.6.2.</p>
  
  <pre><code>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')
  </code></pre>
  
  <p>I have tried using both py and jpy extensions. Tried to change the syntax for importing matplotlib but nothing works.</p>
  
  <p>The error from running with python shows.</p>
  
  <pre><code>Traceback (most recent call last):
    File "example01.py", line 11, in &lt;module&gt;
      get_ipython().magic(u'matplotlib inline')
  AttributeError: 'NoneType' object has no attribute 'magic'
  </code></pre>
  
  <p>Error from running with iPython shows</p>
  
  <pre><code>/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 &lt;module&gt;()
        9 from IPython import get_ipython
       10 
  ---&gt; 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)
  -&gt; 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:
  -&gt; 2095                 result = fn(*args,**kwargs)
     2096             return result
     2097 
  
  &lt;decorator-gen-107&gt; in matplotlib(self, line)
  
  /usr/local/lib/python3.6/site-packages/IPython/core/magic.py in &lt;lambda&gt;(f, *a, **k)
      185     # but it's overkill for just that one bit of state.
      186     def magic_deco(arg):
  --&gt; 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:
  ---&gt; 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 
  -&gt; 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
  --&gt; 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
  --&gt; 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,
  ---&gt; 32                              globals(),locals(),[backend_name],0)
       33 
       34     # Things we pull in from all backends
  
  ModuleNotFoundError: No module named 'ipykernel'
  </code></pre>
  
  <p>After installing ipykernel trying to run the script using ipython gives me followng errors</p>
  
  <pre><code>/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 &lt;module&gt;()
        9 from IPython import get_ipython
       10 
  ---&gt; 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)
  -&gt; 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:
  -&gt; 2095                 result = fn(*args,**kwargs)
     2096             return result
     2097 
  
  &lt;decorator-gen-107&gt; in matplotlib(self, line)
  
  /usr/local/lib/python3.6/site-packages/IPython/core/magic.py in &lt;lambda&gt;(f, *a, **k)
      185     # but it's overkill for just that one bit of state.
      186     def magic_deco(arg):
  --&gt; 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:
  ---&gt; 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
  -&gt; 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 =\
  --&gt; 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:
  ---&gt; 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
  </code></pre>