get_ipython().magic('matplotlib inline') NameError: name 'get_ipython' is not defined
程序员文章站
2022-03-21 10:33:11
...
- 问题描述:
%matplotlib inline 由jupyter notebook 文件转换为 .py 文件之后变为:
get_ipython().magic('matplotlib inline')
- python 执行该 .py 文件,报错如下
get_ipython().magic('matplotlib inline') NameError: name 'get_ipython' is not defined
原文链接
解答大致如下:
Line magics are only supported by the IPython command line. They cannot simply be used inside a script, because %something is not correct Python syntax.
If you want to do this from a script you have to get access to the IPython API and then call the run_line_magic function.
Instead of %matplotlib inline, you will have to do something like this in your script:
from IPython import get_ipython
get_ipython().run_line_magic('matplotlib', 'inline')
意思是该代码只能在 jupyter notebook 或者 ipython 下使用,不可以使用 python直接执行
- 我的解决方法暂为:
注释掉改行代码
# In[2]:
#get_ipython().magic('matplotlib inline')