26

I am trying to import a function from another jupyter notebook

In n1.ipynb:

def test_func(x):
  return x + 1
-> run this

In n2.ipynb:

%%capture
%%run n1.ipynb
test_func(2)

Error:

NameError Traceback (most recent call last)<ipython-input-2-4255cde9aae3> in <module>()
----> 1 test_func(1)

NameError: name 'test_func' is not defined

Any easy ways to do this please?

data_person
  • 4,194
  • 7
  • 40
  • 75
  • @AChampion Yep it didnt work – data_person May 29 '18 at 04:45
  • 1
    Sorry, I have this set up so it works for me - instructions are here: [Importing Notebooks](http://jupyter-notebook.readthedocs.io/en/stable/examples/Notebook/Importing%20Notebooks.html) – AChampion May 29 '18 at 04:48

4 Answers4

25

The nbimporter module helps us here:

pip install nbimporter

For example, with two notebooks in this directory structure:

/src/configuration_nb.ipynb

analysis.ipynb

/src/configuration_nb.ipynb:

class Configuration_nb():
    def __init__(self):
        print('hello from configuration notebook')

analysis.ipynb:

import nbimporter
from src import configuration_nb

new = configuration_nb.Configuration_nb()

output:

Importing Jupyter notebook from ......\src\configuration_nb.ipynb
hello from configuration notebook

We can also import and use modules from python files.

/src/configuration.py

class Configuration():
    def __init__(self):
        print('hello from configuration.py')

analysis.ipynb:

import nbimporter
from src import configuration

new = configuration.Configuration()

output:

hello from configuration.py
Oppy
  • 2,662
  • 16
  • 22
  • Is there a way to automatically run all function definitions in the notebook you are working in? Will `%run notebook.ipynb` work? – BND Jun 26 '19 at 06:26
  • This probably do not work anymore, I tried on colab and on local, can't get it done. – Andres Mitre May 13 '21 at 06:12
  • This allows to access properties of a class that has been defined in another notebook. It does not seem to allow access to arbitrary variables that have been defined there, e.g. configuration_nb.my_variable – Stefan Apr 27 '22 at 07:40
  • Worth noting that the creator of nbimporter has since recommended against using nbimporter https://github.com/grst/nbimporter#update-2019-06-i-do-not-recommend-any-more-to-use-nbimporter – pwb2103 Jul 12 '22 at 05:19
  • There's also `importnb` and `ipynb` , see [here](https://discourse.jupyter.org/t/package-for-importing-jupyter-notebooks-as-modules/12923/2). – Wayne Jun 07 '23 at 16:39
1

Something I've done to import functions into a Jupyter notebook has been to write the functions in a separate Python .py file then use the magic command %run in the notebook. Here's an example of at least one way to do this:

Both notebook.ipynb and helper_functions.py are in the same directory.

helper_functions.py:

def hello_world():
    print('Hello world!')

notebook.ipynb:

%run -i helper_functions.py
hello_world()

notebook.ipynb output:

Hello world!

The %run command tells the notebook to run the specified file and the -i option runs that file in the IPython namespace, which is not really meaningful in this simple example but is useful if your functions interact with variables in the notebook. Check out the docs if I'm not providing enough detail for you.

For what it's worth, I also tried to run function definitions in an outside .ipynb file rather than a outside .py file and it worked for me. Might be worth exploring if you want to keep everything in notebooks.

Kurt Eulau
  • 11
  • 2
0

Based on answer of Kurt:

%run -i configuration.ipynb

This runs another notebook and in the next cell you are able to access the variables defined by that notebook.

Stefan
  • 10,010
  • 7
  • 61
  • 117
0

This works for me

from some_dir.pythonFile import functionName
%run ./some_dir/pythonFile.py

This works as well:

%load_ext autoreload
%autoreload 2
from some_dir.pythonFile import functionName
user1689987
  • 1,002
  • 1
  • 8
  • 23