Load and run python file(.py) in jupyter notebook

Am Ma RaH
2 min readDec 12, 2022

--

Photo by NordWood Themes on Unsplash

In this blog post, we will explore two easy and exciting commands:

  1. The first command is to load a python file(.py) in jupyter notebook
  2. The second command is a magic command that is used to run python file(.py) in jupyter notebook.

Before we start, you may be interested to check out my previous article where we discussed how to convert Jupyter(.ipynb) file to Python(.py) file.

Well, let’s get started!

First, we will see how to load a python file (.py) file in jupyter notebook.

Load a python file (.py) in jupyter notebook:

If you are trying to load the python file(.py) in the same directory as your current jupyter notebook, then you have to use %load file_name.py .

%load file_name.py

Note: file_namereferes to the name of your python file(.py) .

If you are trying to load the python file(.py) from a directory other than your current jupyter notebook, then you have to mention the filepath as well (as shown in the example given below).

%load Users\folder\file_name.py

Now, we will see how to run a python file (.py) in jupyter notebook.

Run python file (.py) in jupyter notebook:

Here, we will explore a magic command (%run) to run a python file(.py) in the jupyter notebook.

If you are trying to run the python file(.py) in the same directory as your current jupyter notebook, then you have to use %run file_name.py .

%run file_name.py

Note: file_namereferes to the name of your python file(.py) .

If you are trying to load the python file(.py) from a directory other than your current jupyter notebook, then you have to mention the filepath as well (as shown in the example given below).

%run Users\folder\file_name.py

Cool! that’s it for this blog.

I hope that this blog post helped you to learn how to load and run a python file (.py) in jupyter notebook with simple and easy python commands.

You may also check out my previous article.

Happy Learning!

--

--

Am Ma RaH

AI Researcher | Software Engineer | Passionate about AI