Last updated: Apr 13, 2024
Reading time·3 min

ipynb fileimport-ipynbimport_ipynb to import a specific function or classThis article covers how to import a Jupyter Notebook ipynb file from another
ipynb file.
Suppose we have the following file called my-notebook.ipynb.
print('bobbyhadz.com')
And the following file called another-notebook.ipynb, stored in the same
directory.
print('another notebook')
To import the another-notebook.ipynb Jupyter Notebook file into your
my-notebook.ipynb file, use the %run magic command.
print('bobbyhadz.com') %run another-notebook.ipynb

The code sample runs the another-notebook.ipynb Jupyter Notebook from
my-notebook.ipynb.
If you get an error when running the command, try to enclose the path to your notebook in double quotes.
For example, wrapping the path in double quotes might be necessary if it contains spaces.
print('bobbyhadz.com') %run "another-notebook.ipynb"
However, note that some operating systems don't seem to process the magic command well when the path is wrapped in double quotes.
The magic %run command has the following format:
%run /path/of/notebook/you/want/to/run
For example, I could also specify the absolute path to the Jupyter Notebook I
want to import after the %run command.
print('bobbyhadz.com') %run /home/borislav/Desktop/bobbyhadz_python/another-notebook.ipynb

Make sure to replace the absolute path in the example with the absolute path to
the .ipynb file you want to import.
ipynb fileYou can use the same approach to import a specific function or class from
another ipynb file.
Suppose we have the following my-notebook.ipynb file.
print('bobbyhadz.com') %run another_notebook.ipynb import greet greet('Bobby')
And the following another_notebook.ipynb file.
print('another notebook') def greet(name): return f'Hello {name}'
Notice that the file name contains an underscore (note a hyphen).
I can now run the my-notebook.ipynb file to invoke the greet() function.

The magic %run command simply runs the specified file inside IPython as a
program.
The filename that is passed to the %run command can be:
.py extension)..ipynb or .ipy extension.If you run into issues, try to restart your Jupyter Notebook server.
import-ipynbYou can also use the import-ipynb
package to import a Jupyter ipynb file from another ipynb file.
First, install the package by running the following command.
pip install import-ipynb # Or with pip3 pip3 install import-ipynb
Now, use the package as follows.
import import_ipynb import another_notebook print('bobbyhadz.com')
And here is my another_notebook.ipynb file.
print('another notebook')

Note that both ipynb files have to be placed in the same directory.
I also renamed the another-notebook.ipynb file to another_notebook.ipynb
(replaced the hyphen with an underscore).
This is necessary because the hyphen is a subtraction operator in Python.
You can read more about the import-ipynb module on
their pypi page.
import_ipynb to import a specific function or classYou can also use the import_ipynb module to import a specific function or
class from another notebook.
Suppose we have the following my-notebook.ipynb file.
import import_ipynb from another_notebook import greet print('bobbyhadz.com') greet('Bobby')
And the following another_notebook.ipynb file.
print('another notebook') def greet(name): return f'Hello {name}'
If you run the my-notebook.ipynb file, you should see the output of the
greet function.

You can learn more about the related topics by checking out the following tutorials: