Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import a function from another .ipynb file

I defined a hello world function in a file called 'functions.ipynb'. Now, I would like to import functions in another file by using "import functions". I am sure that they are in the same folder. However, it still shows that "ImportError: No module named functions". By the way, I am using jupyter notebook. Thanks a lot!

like image 250
Chao Song Avatar asked May 22 '17 14:05

Chao Song


People also ask

How do I use a function from another Ipynb file?

You'll need to install it: pip install ipynb . Create a Notebook named my_functions. ipynb . Add a simple function to it.

How do I import a function from another Jupyter Notebook file?

py file), or Jupyter Notebook. Remember the file that contains the function definitions and the file calling the functions must be in the same directory. To use the functions written in one file inside another file include the import line, from filename import function_name .

Can I import Python function from another file?

Approach: Create a Python file containing the required functions. Create another Python file and import the previous Python file into it. Call the functions defined in the imported file.


4 Answers

You'll want to use the ipynb package/module importer. You'll need to install it: pip install ipynb.

Create a Notebook named my_functions.ipynb. Add a simple function to it.

def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)

Then, create a second IPython Notebook and import this function with:

from ipynb.fs.full.my_functions import factorial

Then you can use it as if it was in the same IPython Notebook:

testing = factorial(5)

See the documentation for more details.

like image 164
David Rinck Avatar answered Sep 21 '22 23:09

David Rinck


For my use case the ipnyb import didn't work for some reason. I had to use Jupyter Notebook magic cell to import my function.:

%run MyOtherNotebook.ipynb     #this is were my function was stored
function(df)                    #then simply run the function
like image 36
Gedas Miksenas Avatar answered Sep 21 '22 23:09

Gedas Miksenas


You can save functions.ipynb as functions.py and can import the file as import functions. Now you can use any function defined in the functions file as functions.function_name For eg, if add is a function,

functions.add(5,3)

after importing will work.

like image 35
Tinto Raj Avatar answered Sep 22 '22 23:09

Tinto Raj


@David Rinck's answer solved the problem, but I'd like to recommend you add the boilerplate __name__ == "__main__" to guard the scripts you don't want to accidentally invoke. It works the same way as in a usual Python file.

If a .ipynb file a.ipynb is imported by another one b.ipynb

from ipynb.fs.full.a import factorial

the __name__ in a.ipynb would be ipynb.fs.full.a rather than "__main__".

like image 44
Lerner Zhang Avatar answered Sep 23 '22 23:09

Lerner Zhang