47

I'd like to import a func from another ipython-notebook. Say,

common_func.ipynb has def func_a()

When I create a new notebook, how can I access the func_a which is from another notebook but in the same folder and same ipython instance?

jtlz2
  • 7,700
  • 9
  • 64
  • 114
shuaiyuancn
  • 2,744
  • 3
  • 24
  • 32
  • 1
    https://stackoverflow.com/q/20186344/478206, https://stackoverflow.com/q/19082397/478206 and https://stackoverflow.com/q/19564625/478206 are all asking the same question. – Robie Basak Dec 05 '17 at 13:58
  • 1
    Please also see this 2019 follow-up on how to selectively import from another notebook: https://stackoverflow.com/q/54317381 – krassowski Jan 23 '19 at 17:01

5 Answers5

28

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()
Erich Mueller
  • 281
  • 1
  • 2
  • 2
  • This flag is not available on the stable version of ipython 1.10. – shuaiyuancn Oct 24 '13 at 13:05
  • I like this solution despite the overhead of extra .py and .pyc files strewn about because it's so easy to implement and doesn't require the extra code from @Jakob's suggestion. – Dannid Feb 04 '16 at 00:49
  • 7
    As of ipython 3.0, the `--script` flag is deprecated. `jupyter nbconvert --to script [notebook]` appears to be the replacement (in 4.1.0). – Curmudgeon Jun 23 '16 at 19:15
  • Where to use `reload()`? It's error: NameError: name 'reload' is not defined – Peter.k Feb 06 '19 at 16:03
28

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)
yeliabsalohcin
  • 720
  • 1
  • 6
  • 14
  • does this still work? I'm having the below error the second time I reload: reload(): module not in sys.modules – masotann Feb 26 '17 at 23:46
  • I agree there is something odd with `reload()` - on second run it spits out _not in sys.modules_ error. Solved for me by adding the `import notebookName as name` with `reload(notebookName)` and running twice. – yeliabsalohcin May 16 '17 at 11:03
  • 2
    And where does reload() come from? – Stephan Schielke Jun 28 '17 at 14:43
  • This overflow has some information about reload, which is a default part of python 2.x: https://stackoverflow.com/questions/437589/how-do-i-unload-reload-a-python-module – yeliabsalohcin Jul 03 '17 at 09:35
13

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!

Fabian Rost
  • 2,284
  • 2
  • 15
  • 27
Jakob
  • 19,815
  • 6
  • 75
  • 94
  • 2
    Specifically, the link is currently [here](http://nbviewer.ipython.org/github/ipython/ipython/blob/master/examples/Notebook/Importing%20Notebooks.ipynb). – Mike Apr 04 '14 at 18:36
  • 3
    or [here](http://nbviewer.ipython.org/github/ipython/ipython/blob/master/examples/IPython%20Kernel/Importing%20Notebooks.ipynb) – Ben Usman Oct 04 '15 at 02:23
  • 4
    Gist with that code: https://gist.github.com/alexoliveira/ae5c896d3b065ecccdb170f5609673a4. To use it, put `notebook_importer.py` in your iPython root. Import it using `from notebook_importer import *`. Finally import your notebook! (ex: my_notebook.ipynb with `import my_notebook`). Now all your functions/classes in that notebook file are accessible. – Alexandre Jul 21 '16 at 22:41
  • An alternative method is to use the %run magic function (from https://blog.sicara.com/present-data-science-results-jupyter-notebook-import-into-another-108433bc8505) – Flavian Hautbois Sep 25 '17 at 14:31
13

Another option is ipynb. A nice benefit over nbimporter is that it can import just the definitions, while not executing the rest of the code. It's fairly new at this point (0.5).

pip install ipynb

And then:

import ipynb.fs.defs.my_other_notebook as other
Daniel Darabos
  • 26,991
  • 10
  • 102
  • 114
  • However, this may not work if the notebook contains ipython magics: https://ipynb.readthedocs.io/en/latest/ – BND Jun 21 '19 at 09:50
  • doesn't work in colab ImportError: Could not import .... for ipynb.fs.full.polygon: incorrect version or language – gianni Jun 18 '23 at 19:28
1

importnb is an option. It has some nice features like fuzzy matching to help you find your notebook and aiding in importing notebooks where the name would normally be an issue.

Example adapted from OP

Imagine you have a notebook named common_func.ipynb with the following function saved in it:

def func_a(x):
    return x + 67

Then you are working in a new notebook, Untitled1.ipynb.

To use func_a in the current notebook Untitled1.ipynb, in a cell run %pip install importnb. That will install the importnb package in the current environment backing the current notebook. (It won't do anything if you already have importnb installed.) You are welcome to install the package your favorite way as well.

Put common_func.ipynb or a copy of it in the current working directory where you are saving the current notebook Untitled1.ipynb.

Then in the current notebook you can use importnb to import the notebook common_func.ipynb and use func_a from it, like so:

from importnb import Notebook
with Notebook(): 
    import common_func
common_func.func_a(2)
Wayne
  • 6,607
  • 8
  • 36
  • 93