Importing IPython Notebooks as Modules

Wouldn't it be cool to import Jupyter Notebook just as a Python module? Well, there is a convenient way, you just need to add one line to your Jupyter configuration to execute several Python functions. So, do:

1. Generate the profile files.

$ jupyter notebook --generate-config

/home/you/.jupyter/jupyter_notebook_config.py

2. Edit the jupyter_notebook_config.py to append:

c.InteractiveShellApp.exec_files = [ "/home/you/.jupyter/notebook_finder.py" ]

Where the runatstartup.py should be the path to the following file:

notebook_finder.py (gist).

Then, you will be able to import other Jupyter notebooks just like Python libraries :)

3. N.B.:

Additionally, you just need an empty __init__.py file in your root folder of IPython Notebook, and in every deeper level folder which you want to import from. With that, if you have a folder named "nbpackage" and "mynotebook.ipynb" inside it, you can do:

from nbpackage import mynotebook

Or simply

import mynotebook
mynotebook.function()

if you are in the folder already. Works just like Python package imports.

F & P