Error executing Python script AttributeError: 'NoneType' object has no attribute 'run_line_magic'

一笑奈何 提交于 2019-12-12 05:38:34

问题


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

import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from IPython import get_ipython
ipython_shell = get_ipython()
get_ipython().run_line_magic('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 "LR.jpy", line 7, in <module>
    get_ipython().run_line_magic('matplotlib', 'inline')
AttributeError: 'NoneType' object has no attribute 'run_line_magic'

Error from running with iPython shows

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
C:\Users\Roland\Google Drive\Work\Machine Learning\machine-learning-ex1\ex1\LR.jpy in <module>()
      5 from IPython import get_ipython
      6 ipython_shell = get_ipython()
----> 7 get_ipython().run_line_magic('matplotlib', 'inline')

c:\program files\python\lib\site-packages\IPython\core\interactiveshell.pyc in run_line_magic(self, magic_name, line)
   2077                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2078             with self.builtin_trap:
-> 2079                 result = fn(*args,**kwargs)
   2080             return result
   2081

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

c:\program files\python\lib\site-packages\IPython\core\magic.pyc in <lambda>(f, *a, **k)
    186     # but it's overkill for just that one bit of state.
    187     def magic_deco(arg):
--> 188         call = lambda f, *a, **k: f(*a, **k)
    189
    190         if callable(arg):

c:\program files\python\lib\site-packages\IPython\core\magics\pylab.pyc in matplotlib(self, line)
     98             print("Available matplotlib backends: %s" % backends_list)
     99         else:
--> 100             gui, backend = self.shell.enable_matplotlib(args.gui)
    101             self._show_matplotlib_backend(args.gui, backend)
    102

c:\program files\python\lib\site-packages\IPython\core\interactiveshell.pyc in enable_matplotlib(self, gui)
   2945                 gui, backend = pt.find_gui_and_backend(self.pylab_gui_select)
   2946
-> 2947         pt.activate_matplotlib(backend)
   2948         pt.configure_inline_support(self, backend)
   2949

c:\program files\python\lib\site-packages\IPython\core\pylabtools.pyc in activate_matplotlib(backend)
    293
    294     import matplotlib.pyplot
--> 295     matplotlib.pyplot.switch_backend(backend)
    296
    297     # This must be imported last in the matplotlib series, after

c:\program files\python\lib\site-packages\matplotlib\pyplot.pyc in switch_backend(newbackend)
    230     matplotlib.use(newbackend, warn=False, force=True)
    231     from matplotlib.backends import pylab_setup
--> 232     _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
    233
    234

c:\program files\python\lib\site-packages\matplotlib\backends\__init__.pyc 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

ImportError: No module named ipykernel.pylab.backend_inline

EDIT:

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

---------------------------------------------------------------------------
UnknownBackend                            Traceback (most recent call last)
C:\Users\Roland\Google Drive\Work\Machine Learning\machine-learning-ex1\ex1\LR.py in <module>()
      5 from IPython import get_ipython
      6 ipython_shell = get_ipython()
----> 7 get_ipython().run_line_magic('matplotlib', 'inline')

c:\program files\python\lib\site-packages\IPython\core\interactiveshell.pyc in run_line_magic(self, magic_name, line)
   2077                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2078             with self.builtin_trap:
-> 2079                 result = fn(*args,**kwargs)
   2080             return result
   2081

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

c:\program files\python\lib\site-packages\IPython\core\magic.pyc in <lambda>(f, *a, **k)
    186     # but it's overkill for just that one bit of state.
    187     def magic_deco(arg):
--> 188         call = lambda f, *a, **k: f(*a, **k)
    189
    190         if callable(arg):

c:\program files\python\lib\site-packages\IPython\core\magics\pylab.pyc in matplotlib(self, line)
     98             print("Available matplotlib backends: %s" % backends_list)
     99         else:
--> 100             gui, backend = self.shell.enable_matplotlib(args.gui)
    101             self._show_matplotlib_backend(args.gui, backend)
    102

c:\program files\python\lib\site-packages\IPython\core\interactiveshell.pyc in enable_matplotlib(self, gui)
   2950         # Now we must activate the gui pylab wants to use, and fix %run to take
   2951         # plot updates into account
-> 2952         self.enable_gui(gui)
   2953         self.magics_manager.registry['ExecutionMagics'].default_runner = \
   2954             pt.mpl_runner(self.safe_execfile)

c:\program files\python\lib\site-packages\IPython\terminal\interactiveshell.pyc in enable_gui(self, gui)
    450     def enable_gui(self, gui=None):
    451         if gui:
--> 452             self._inputhook = get_inputhook_func(gui)
    453         else:
    454             self._inputhook = None

c:\program files\python\lib\site-packages\IPython\terminal\pt_inputhooks\__init__.pyc in get_inputhook_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 u'inline'. Supported event loops are: qt, qt4, qt5, gtk, gtk2, gtk3, tk, wx, pyglet, glut, osx

回答1:


In the first case, you only get back a non-none instance if you are running IPython

Help on function get_ipython in module IPython.core.getipython:

get_ipython()
    Get the global InteractiveShell instance.

    Returns None if no InteractiveShell instance is registered.

In the second case, the inline backend is shipped as part of ipykernel which you may not have installed. The inline backend only works in the notebook and qtconsole, not the normal prompt.




回答2:


My problem was in terminal it is impossible to show the plot images. I needed to use jupyter notebook. I installed jupyter notebook and ran it with the following command.

jupyter notebook

After that a new tab in the browser opens. There is a New button in the top right corner.

There I selected the python 2. In a new tab there are cells. And on each cell, codes can be pasted and run. That solved the problem for me.



来源:https://stackoverflow.com/questions/40721660/error-executing-python-script-attributeerror-nonetype-object-has-no-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!