执行Python脚本AttributeError时出错:';非类型';对象没有属性';运行"魔术"';
我在下面的python代码中遇到错误。我是根据剧本跑的。我正在使用python 2.7.12执行Python脚本AttributeError时出错:';非类型';对象没有属性';运行"魔术"';,python,matplotlib,ipython,Python,Matplotlib,Ipython,我在下面的python代码中遇到错误。我是根据剧本跑的。我正在使用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') 我尝试过使用
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')
我尝试过使用py和jpy扩展。试图更改导入matplotlib的语法,但没有任何效果
使用python运行时出现的错误如下
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'
---------------------------------------------------------------------------
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
回溯(最近一次呼叫最后一次):
文件“LR.jpy”,第7行,在
get_ipython()。运行_line_magic('matplotlib','inline')
AttributeError:“非类型”对象没有属性“run\u line\u magic”
使用iPython 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'
---------------------------------------------------------------------------
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
---------------------------------------------------------------------------
ImportError回溯(最近一次呼叫最后一次)
C:\Users\Roland\Google Drive\Work\Machine Learning\Machine-Learning-ex1\ex1\LR.jpy in()
5从IPython导入获取
6 ipython_shell=get_ipython()
---->7 get_ipython()。运行_line_magic('matplotlib','inline')
c:\program files\python\lib\site packages\IPython\core\interactiveshell.pyc in run\u line\u magic(self,magic\u name,line)
2077 kwargs['local\u ns']=sys.\u getframe(堆栈深度).f\u locals
2078带自建存水弯:
->2079结果=fn(*args,**kwargs)
2080返回结果
2081
在matplotlib中(自身,行)
(f,*a,**k)中的c:\program files\python\lib\site packages\IPython\core\magic.pyc
186#但仅仅为了那一点点国家就太过分了。
187 def魔术装饰(arg):
-->188调用=λf,*a,**k:f(*a,**k)
189
190如果可调用(arg):
matplotlib中的c:\program files\python\lib\site packages\IPython\core\magics\pylab.pyc(self,line)
98打印(“可用matplotlib后端:%s”%backends\u列表)
99其他:
-->100 gui,后端=self.shell.enable_matplotlib(args.gui)
101 self.\u show\u matplotlib\u后端(args.gui,后端)
102
c:\program files\python\lib\site packages\IPython\core\interactiveshell.pyc在enable_matplotlib(self,gui)中
2945 gui,backend=pt.find\u gui\u和\u backend(self.pylab\u gui\u select)
2946
->2947点激活_matplotlib(后端)
2948 pt.配置\u内联\u支持(自身、后端)
2949
c:\program files\python\lib\site packages\IPython\core\pylabtools.pyc在activate_matplotlib(后端)中
293
294导入matplotlib.pyplot
-->295 matplotlib.pyplot.switch_后端(后端)
296
297#这必须在matplotlib系列中的最后一个导入,在
交换机后端(newbackend)中的c:\program files\python\lib\site packages\matplotlib\pyplot.pyc
230 matplotlib.use(newbackend,warn=False,force=True)
231从matplotlib.backends导入pylab_设置
-->232(后端)模块(mod),新(图形)管理器(manager),如果互动(draw),(show=pylab)设置
233
234
pylab_setup()中的c:\program files\python\lib\site packages\matplotlib\backends\\uuuuu init\uuuuu.pyc
30#进口。0表示仅执行绝对导入。
31 backend\u mod=\u\u导入\u(backend\u名称,
--->32全局变量(),局部变量(),[backend_name],0)
33
34#我们从所有后端获取的东西
ImportError:没有名为ipykernel.pylab.backend_inline的模块
编辑:
安装ipykernel后,尝试使用ipython运行脚本时会出现以下错误
---------------------------------------------------------------------------
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
---------------------------------------------------------------------------
UnknownBackend回溯(最近一次呼叫最后一次)
C:\Users\Roland\Google Drive\Work\Machine Learning\Machine-Learning-ex1\ex1\LR.py in()
5从IPython导入获取
6 ipython_shell=get_ipython()
---->7 get_ipython()。运行_line_magic('matplotlib','inline')
c:\program files\python\lib\site packages\IPython\core\interactiveshell.pyc in run\u line\u magic(self,magic\u name,line)
2077 kwargs['local\u ns']=sys.\u getframe(堆栈深度).f\u locals
2078带自建存水弯:
->2079结果=fn(*args,**kwargs)
2080返回结果
2081
在matplotlib中(自身,行)
(f,*a,**k)中的c:\program files\python\lib\site packages\IPython\core\magic.pyc
186#但仅仅为了那一点点国家就太过分了。
187 def魔术装饰(arg):
-->188调用=λf,*a,**k:f(*a,**k)
189
190如果可调用(arg):
matplotlib中的c:\program files\python\lib\site packages\IPython\core\magics\pylab.pyc(self,line)
98打印(“可用matplotlib后端:%s”%backends\u列表)
99其他:
-->100 gui,后端=self.shell.enable_matplotlib(args.gui)
101 self.\u show\u matplotlib\u后端(args.gui,后端)
102
c:\program files\python\lib\site packages\IPython\core\interactiveshell.pyc在enable_matplotlib(self,gui)中
2950#现在我们必须激活pylab想要使用的gui,并修复%run
2951#考虑到绘图更新
->2952自启用图形用户界面(gui)
2953 self.magics\u manager.registry['ExecutionMagics']。默认运行程序=\
2954 pt.mpl\u runner(self.safe\u execfile)
enable_gui(self,gui)中的c:\program files\python\lib\site packages\IPython\terminal\interactiveshell.pyc
450 def enable_gui(自身,gui=None):
451如果gui:
-->452 self.\u inputhook=get\u inputhook\u func(gui)
453其他:
454 self.\u inputhook=无
c:\program files\python\lib\site packages\IPython\terminal\pt\u inputhooks\\u init\u.pyc在get\u inputhook\u func(gui)中
36
37如果gui不在后端:
--->38升起未知背景(gui)
39
40如果gui使用别名:
UnknownBackend:u'inline'没有事件循环集成。支持的事件循环有:qt、qt4、qt5、gtk、gtk2、gtk3、tk、wx、pyglet、glut、osx
在第一种情况下,您只返回一个非none instanc