66

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?

dyno8426
  • 1,179
  • 2
  • 13
  • 22

4 Answers4

86

You have to run your script with ipython:

$ ipython python/my_test_imagenet.py

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.

beezz
  • 2,398
  • 19
  • 15
  • 4
    I tried the above but it gives the following error: "UsageError: Invalid GUI request u'inline', valid ones are: pyglet, osx, qt5, qt, glut, gtk, gtk3, tk, wx". – dyno8426 Sep 14 '15 at 11:05
  • 6
    @AdarshChauhan use 'matplotlib auto' instead of 'matplotlib inline'. – Syrtis Major Jan 19 '16 at 08:46
  • @beezz How can I test (inside a script) if my scipt has been launched by `ipython` instead of `python` ? – SebMa Jul 10 '17 at 13:53
  • 1
    @SebMa you can catch ``NameError`` in plain python caused by ``get_ipython`` not present – beezz Jul 13 '17 at 22:52
  • 3
    This is coming from the output of `jupyter nbconvert`... why would it create a file that doesn't run? Seems to defeat the purpose of that command. Further, why is the output beginning with `#!/bin/env python` if it was really intended to be run with `ipython`? – MichaelChirico Jul 28 '19 at 14:15
23

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:

plt.show(block=True)
Shital Shah
  • 63,284
  • 17
  • 238
  • 185
10

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
Vincenzooo
  • 2,013
  • 1
  • 19
  • 33
1

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 (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)

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Francisco Maria Calisto Jul 06 '22 at 13:01