Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing an ipynb file from another ipynb file?

It is really simple in newer Jupyter:

%run MyOtherNotebook.ipynb

Install my helper library from the command prompt:

pip install import-ipynb

Import it from your notebook:

import import_ipynb

Now import your .ipynb notebook as if it was a .py file

import TheOtherNotebook

This python-ipynb module is just one file and it strictly adheres to the official howto on the jupyter site.

PS It also supports things like from A import foo, from A import * etc

PPS Works with subdirectories: import A.B


Run

!pip install ipynb

and then import the other notebook as

from ipynb.fs.full.<notebook_name> import *

or

from ipynb.fs.full.<notebook_name> import <function_name>

Make sure that all the notebooks are in the same directory.

Edit 1: You can see the official documentation here - https://ipynb.readthedocs.io/en/stable/

Also, if you would like to import only class & function definitions from a notebook (and not the top level statements), you can use ipynb.fs.defs instead of ipynb.fs.full. Full uppercase variable assignment will get evaluated as well.


Install ipynb from your command prompt

pip install import-ipynb

Import in your notebook file

import import_ipynb

Now use regular import command to import your file

import MyOtherNotebook

You can use import nbimporter then import notebookName


The above mentioned comments are very useful but they are a bit difficult to implement. Below steps you can try, I also tried it and it worked:

  1. Download that file from your notebook in PY file format (You can find that option in File tab).
  2. Now copy that downloaded file into the working directory of Jupyter Notebook
  3. You are now ready to use it. Just import .PY File into the ipynb file