NameError:未定义名称“get_ipython”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32538758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:44:42  来源:igfitidea点击:

NameError: name 'get_ipython' is not defined

pythonipythoncaffepycaffe

提问by dyno8426

I am working on Caffe framework and using PyCaffe interface. I am using a Python script obtained from converting the IPython Notebook 00-classification.ipynbfor testing the classification by a trained model for ImageNet. But any get_ipython()statement in the script is giving the following error:

我正在研究 Caffe 框架并使用 PyCaffe 接口。我正在使用通过转换 IPython Notebook 00-classification.ipynb获得的 Python 脚本,通过ImageNet 的训练模型测试分类。但是脚本中的任何get_ipython()语句都会出现以下错误:

$ python python/my_test_imagenet.py 
Traceback (most recent call last):
  File "python/my_test_imagenet.py", line 23, in <module>
    get_ipython().magic(u'matplotlib inline')

In the script, I'm importing the following:

在脚本中,我正在导入以下内容:

import numpy as np
import matplotlib.pyplot as plt

get_ipython().magic(u'matplotlib inline')

# Make sure that caffe is on the python path:
caffe_root = '/path/to/caffe/'
import sys
sys.path.insert(0, caffe_root + 'python')

import caffe

plt.rcParams['figure.figsize'] = (10, 10)
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'

import os

# ... Rest of the code...

Can someone please help me to resolve this error?

有人可以帮我解决这个错误吗?

采纳答案by beezz

You have to run your script with ipython:

你必须用 ipython 运行你的脚本:

$ ipython python/my_test_imagenet.py

Then get_ipythonwill be already in global context.

然后get_ipython将已经在全球范围内。

Note: Importing it via from IPython import get_ipythonin ordinary shell pythonwill not work as you really need ipythonrunning.

注意:通过from IPython import get_ipython在普通 shell 中导入它是python行不通的,因为您确实需要ipython运行。

回答by Shital Shah

If your intention is to run converted .py file notebook then you should just comment out get_ipython()statements. The matlibplot output can't be shown inside console so you would have some work to do . Ideally, iPython shouldn't have generated these statements. You can use following to show plots:

如果您打算运行转换后的 .py 文件笔记本,那么您应该只注释掉get_ipython()语句。matlibplot 输出无法显示在控制台中,因此您需要做一些工作。理想情况下,iPython 不应该生成这些语句。您可以使用以下内容来显示图表:

plt.show(block=True)

回答by Vincenzooo

get_ipythonis available only if the IPython module was imported that happens implicitly if you run ipython shell (or Jupyter notebook).

get_ipython仅当您运行 ipython shell(或 Jupyter notebook)时隐式导入 IPython 模块时才可用。

If not, the import will fail, but you can still import it explicitly with:

如果没有,导入将失败,但您仍然可以使用以下命令显式导入它:

from IPython import get_ipython