Import Jupyter ipynb file from another ipynb file

avatar
Borislav Hadzhiev

Last updated: Apr 13, 2024
3 min

banner

# Table of Contents

  1. Import a Jupyter ipynb file from another ipynb file
  2. Importing a specific function or class from another ipynb file
  3. Import a Jupyter ipynb file from another ipynb file using import-ipynb
  4. Using import_ipynb to import a specific function or class

# Import a Jupyter ipynb file from another ipynb file

This article covers how to import a Jupyter Notebook ipynb file from another ipynb file.

Suppose we have the following file called my-notebook.ipynb.

my-notebook.ipynb
print('bobbyhadz.com')

And the following file called another-notebook.ipynb, stored in the same directory.

another-notebook.ipynb
print('another notebook')

To import the another-notebook.ipynb Jupyter Notebook file into your my-notebook.ipynb file, use the %run magic command.

my-notebook.ipynb
print('bobbyhadz.com') %run another-notebook.ipynb

import jupyter notebook ipynb file from another ipynb file

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.

my-notebook.ipynb
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:

my-notebook.ipynb
%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.

my-notebook.ipynb
print('bobbyhadz.com') %run /home/borislav/Desktop/bobbyhadz_python/another-notebook.ipynb

import jupyter ipynb file from another ipynb file absolute path

Make sure to replace the absolute path in the example with the absolute path to the .ipynb file you want to import.

# Importing a specific function or class from another ipynb file

You 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.

my-notebook.ipynb
print('bobbyhadz.com') %run another_notebook.ipynb import greet greet('Bobby')

And the following another_notebook.ipynb file.

another_notebook.ipynb
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.

import specific function or class from another ipynb file

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:

  1. A pure Python script (with a .py extension).
  2. A file with custom IPython syntax (such as magic functions). In this case, the file can either have a .ipynb or .ipy extension.

If you run into issues, try to restart your Jupyter Notebook server.

# Import a Jupyter ipynb file from another ipynb file using import-ipynb

You 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.

shell
pip install import-ipynb # Or with pip3 pip3 install import-ipynb

Now, use the package as follows.

my-notebook.ipynb
import import_ipynb import another_notebook print('bobbyhadz.com')

And here is my another_notebook.ipynb file.

another_notebook.ipynb
print('another notebook')

import jupyter notebook ipynb file from another notebook using import ipynb

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.

# Using import_ipynb to import a specific function or class

You 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.

my-notebook.ipynb
import import_ipynb from another_notebook import greet print('bobbyhadz.com') greet('Bobby')

And the following another_notebook.ipynb file.

another_notebook.ipynb
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.

using import ipynb to import function or class from another ipynb file

# Additional Resources

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

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.