AttributeError:“NoneType”对象没有“magic”属性

2024-04-26 21:59:48 发布

您现在位置:Python中文网/ 问答频道 /正文

下面的python代码有错误。我在逃避剧本。我使用的是Python3.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')

我试过同时使用py和jpy扩展。试图更改导入matplotlib的语法,但没有任何工作。

使用python运行时出现错误。

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'

运行iPython时出错

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

在安装了ipykernel并尝试使用ipython运行脚本之后,会出现以下错误

/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

Tags: nameinpycoreselfbackendmatplotliblib