How to Import Python File as Module in Jupyter Notebook

In this blog, learn how to streamline your data science workflow by importing Python files as modules in Jupyter notebooks. Explore efficient techniques for utilizing functions, classes, and variables from your existing scripts, enhancing your productivity.

How to Import Python File as Module in Jupyter Notebook

As a data scientist or software engineer, one of the most common tasks you would encounter is importing modules in Jupyter notebook. This is especially true when you have written a Python script that contains functions, classes, or variables that you need to use in your notebook. In this article, we will discuss how to import a Python file as a module in Jupyter notebook.

What is a module in Python?

A module in Python is simply a file containing Python definitions and statements. These definitions and statements can be classes, functions, variables, or even other modules. The file can have any name with a .py extension and can be imported into other Python programs or scripts.

Why Import Python Files as Module in Jupyter Notebook?

Jupyter notebook is an interactive computational environment that allows you to create and share documents that contain live code, equations, visualizations, and narrative text. One of the benefits of using Jupyter notebook is that it allows you to import Python files as modules, which means you can reuse code you have written in other Python scripts or modules.

Importing Python files as modules in Jupyter notebook also helps to keep your notebook organized and easy to read. Instead of having a long and messy notebook that contains all your code, you can separate your code into different Python files and import them as modules into your notebook.

How to Import Python File as Module in Jupyter Notebook

To import a Python file as a module in Jupyter notebook, you need to follow these simple steps:

  1. Create a Python file containing the code you want to import
  2. Save the Python file with a .py extension
  3. Move the Python file to the same directory as your Jupyter notebook
  4. Import the Python file as a module in your Jupyter notebook

Step 1: Create a Python file containing the code you want to import

The first step is to create a Python file containing the code you want to import. This can be a script containing functions, classes, or variables. For example, let’s create a Python file named my_module.py containing a function that adds two numbers:

def add_numbers(a, b):
    return a + b

Step 2: Save the Python file with a .py extension

The next step is to save the Python file with a .py extension. This is important because Python will only recognize files with a .py extension as Python modules.

Step 3: Move the Python file to the same directory as your Jupyter notebook

The Python file you created in step 1 should be moved to the same directory as your Jupyter notebook. This is important because Python will only look for modules in the current working directory. Alt text

Step 4: Import the Python file as a module in your Jupyter notebook

To import the Python file as a module in your Jupyter notebook, you need to use the import keyword followed by the name of your Python file without the .py extension. For example, to import the my_module.py file we created earlier, we would use the following code in our Jupyter notebook:

import my_module

After importing the Python file as a module, you can then use any function, class, or variable defined in the file in your notebook. For example, to use the add_numbers function we defined earlier, we would use the following code in our Jupyter notebook:

result = my_module.add_numbers(2, 3)
print(result)

This would output 5, which is the result of adding 2 and 3 using the add_numbers function defined in our my_module.py file.

Conclusion

Importing Python files as modules in Jupyter notebook is a simple and powerful way to reuse code you have written in other Python scripts or modules. By following the steps outlined in this article, you can easily import your Python files as modules and use them in your Jupyter notebook. This helps to keep your notebook organized and easy to read, and makes it easier to share your code with others.


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.