How to Import Functions from Another Jupyter Notebook

In this blog, discover a seamless method for importing functions across Jupyter notebooks, a vital skill for data scientists and software engineers managing multiple notebooks within a single project.

As a data scientist or software engineer, you may find yourself working with multiple Jupyter notebooks for a single project. In such cases, it is often necessary to import functions from one notebook to another. Fortunately, Jupyter notebooks provide an easy and efficient way to do so. In this article, we will discuss how to import functions from another Jupyter notebook.

What is a Jupyter Notebook?

Before we dive into importing functions from another notebook, it is important to understand what a Jupyter notebook is. A Jupyter notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations, and narrative text. The notebook consists of cells, which can contain code, markdown text, or raw text.

How to Import Functions from Another Jupyter Notebook

Importing functions from another Jupyter notebook is a simple process that involves three steps:

  1. Create a Jupyter notebook that contains the functions you want to import.
  2. Save the notebook with a .ipynb extension.
  3. Import the functions from the notebook using the %run magic command.

Step 1: Create a Jupyter Notebook with Functions

In order to import functions from another notebook, you must first create a notebook that contains the functions you want to import. To do this, create a new notebook and define the functions you want to use in that notebook.

# Define a function that returns the sum of two numbers
def add_numbers(a, b):
    return a + b

# Define a function that returns the difference between two numbers
def subtract_numbers(a, b):
    return a - b

Alt text

Step 2: Save the Notebook with a .ipynb Extension

Once you have defined the functions in a notebook, you must save the notebook with a .ipynb extension. This will allow you to import the functions from another notebook using the %run magic command.

Step 3: Import the Functions from the Notebook Using the %run Magic Command

To import the functions from the notebook, use the %run magic command followed by the name of the notebook you want to import from. For example, if you want to import the functions from a notebook called Saturn2.ipynb, you would use the following command:

%run Saturn2.ipynb

After running this command, you will be able to use the functions defined in the Saturn2.ipynb notebook. For example, you can call the add_numbers function as follows:

result = add_numbers(2, 3)
print(result) # Output: 5

Output:

5

Similarly, you can call the subtract_numbers function as follows:

result = subtract_numbers(5, 3)
print(result) # Output: 2

Output:

2

Conclusion

Importing functions from another Jupyter notebook is a simple process that can save you a lot of time and effort when working on a project. By following the steps outlined in this article, you can easily import functions from another notebook and use them in your current notebook. This can help you organize your code more efficiently and improve your workflow.


About Saturn Cloud

Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Request a demo today to learn more.