NameError: name 'get_ipython' is not defined


Python Problem Overview

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

$ python python/ 
Traceback (most recent call last):
  File "python/", 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?

Python Solutions

Solution 1 - Python

You have to run your script with ipython:

$ ipython python/

Then get_ipython will be already in global context.

Note: Importing it via from IPython import get_ipython in ordinary shell python will not work as you really need ipython running.

Solution 2 - Python

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:

Solution 3 - Python

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

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

from IPython import get_ipython


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiondyno8426View Question on Stackoverflow
Solution 1 - PythonbeezzView Answer on Stackoverflow
Solution 2 - PythonShital ShahView Answer on Stackoverflow
Solution 3 - PythonVincenzoooView Answer on Stackoverflow