How to Execute a py file from a ipynb file on the Jupyter Notebook

In this blog, explore executing Python scripts from Jupyter notebooks for seamless data analysis and visualization. Learn how to run .py files within your .ipynb projects, enhancing your data science workflow.

As a data scientist, you often work with Jupyter notebooks to perform data analysis and create visualizations. However, there may be times when you need to execute a Python script from within a Jupyter notebook. In this blog post, we will discuss how to execute a *.py file from a *.ipynb file on the Jupyter notebook.

Overview

Jupyter notebooks are a popular tool among data scientists for performing data analysis and creating visualizations. They allow you to write and execute code in a web-based interface, making it easy to collaborate with others and share your work.

However, there may be times when you need to execute a Python script from within a Jupyter notebook. This may be because the script contains functionality that is not available in the notebook environment, or because you want to keep your code organized in separate files.

Fortunately, executing a *.py file from a *.ipynb file on the Jupyter notebook is a straightforward process.

Prerequisites

Before we dive into the steps for executing a Python script from within a Jupyter notebook, there are a few prerequisites that you need to have in place:

  • Jupyter notebook installed on your system
  • A Python script (*.py file) that you want to execute
  • A Jupyter notebook (*.ipynb file) that will call the Python script

Assuming you have these prerequisites in place, let’s move on to the steps for executing a Python script from within a Jupyter notebook.

Using the %run command

%run /path/to/my_script.py

This is the the approach we recommend, because doing so will execute the script in the interactive namespace. This means that after the script is done executing, you can interact with the variables inside the notebook.

For example if you have the following script:

import numpy as np

if __name__ == "__main__":
    data = np.random.random((5, 5))

after executing

%run /path/to/my_script.py

I can inspect the value of data

running a script

Using subprocess

Using subprocess is useful if you are writing a Python application or library that needs to call a script.

If we modify the above script to print the array of random numbers, and then we call this:

import subprocess
subprocess.run("python script.py", capture_output=True, shell=True)

we get

CompletedProcess(args='python script.py', returncode=0, stdout=b'[[0.85737546 0.64604192 0.60818598 0.41507123 0.6947373 ]\n [0.02669446 0.83222652 0.23495144 0.78425423 0.42913627]\n [0.2005467  0.8932773  0.4331022  0.77233348 0.72876355]\n [0.89785946 0.90947737 0.89053991 0.62614878 0.29100697]\n [0.24544874 0.84813489 0.45980812 0.58959866 0.51044578]]\n', stderr=b'')

Using the ! operator

the exclamation mark dispatches the contents of the cell as a shell command

!python script.py

running a script

This operator is useful when you don’t want the script to pollute your interactive namespace. It is also used to execute shell commands that have nothing to do with Python.

Conclusion

In conclusion, executing a *.py file from a *.ipynb file on the Jupyter notebook is a simple process that can be completed in just a few steps. There are several different approaches, depending on your use case.


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.