How can I import from another ipython-notebook?

There is now a dedicated function to achieve this called nbimporter - installed via pip.

Usage:

import nbimporter
import notebookName as name
name.functionName()

And if you update notebookName.ipynb then reload by:

reload(name)

or this for Python 3 (reload not included by default):

from importlib import reload
reload(name)

In the IPython mailing list this was discussed very recently, see here. Finally (here), an example notebook was found, which shows a way to import code from other notebooks. This notebook can be found in the examples/notebooks directory, and looks like this. You 'just' have to define the NotebookLoader and NotebookFinder classes as shown in the notebook. I've tried with IPython 1.1.0 and it works fine!


When you start ipython use the --script flag: For example

ipython notebook --script

Then whenever you save your notebook "common_func.ipnb" it will also create a file entitled "common_func.py." You can import functions from that by using

from common_func import func_a

If you change the common_func notebook, you may need to use

reload()