Jupyter Notebook

Import a personal lib

Importing the .ipynb file we wrote as a module to a new .ipynb file is different from importing them in a .py file. The easiest way is to convert those .ipynb file to a .py file and you can import those in a new .ipynb file. However, this method doesn’t allow you to modify the code and reload it easily.

When the .ipynb lib file is in the same folder with you

For example, the lib’s name is test.ipynb. There is a function called hello() within it. In a new .ipynb file, you need to type the following.

1
2
3
4
5
6
7
# !pip install import-ipynb
import import_ipynb
import importlib
importlib.reload(test)
import test

test.hello()

The import_ipynb lib is allowing you to import a .ipynb file. The importlib file is allowing you to reload the module thus the new change to the lib file can be reloaded into your file.

When the .ipynb lib file is in the subfolder of current folder

For example, the lib file in the folder of lib. The lib’s name is test.ipynb. There is a function called hello() within it. In a new .ipynb file, you need to type the following.