Question: 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/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?

Answer: 
`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

```
Just want to add that converting ipynb file to py having magic functions in your script trigger the same error as well since for instance `%%time` converts to `get_ipython().run_cell_magic('time')`

Why so? [Magic functions](https://ipython.readthedocs.io/en/stable/interactive/python-ipython-diff.html?highlight=magics) (magics) are often present in the form of shell-like syntax, but they are python functions under the hood.

The conversion from cell magics to get\_ipython() commands is a part of nbconvert and is required to get a runnable python script, as cell magics are not valid python outside of a notebook cell(things like magics or aliases are turned into function calls)