Back to articles list Articles
8 minutes read

Python Scripts vs. Jupyter Notebooks: Pros and Cons

Learn the ins and outs of Python scripts and Jupyter Notebooks, including when to use each one.

There are many tools available to Python developers. Since this language’s early days, Python scripts have been the canonical way of writing and sharing Python code. But in the past few years, Jupyter Notebooks have gained considerable recognition among data scientists as a powerful tool for testing ideas and sharing results. But what exactly sets Jupyter Notebooks apart from Python scripts? What are the advantages and disadvantages of each, and when would you want to use one or the other?

In this article, we will explore the pros and cons of Python scripts and Jupyter Notebooks. We will start by defining exactly how Python scripts and Jupyter Notebooks work and then move on to compare their strengths and weaknesses. Let’s get right into it!

What Are Python Scripts?

Simply put, a Python script is a plain text file ending with the .py extension. You can open, edit, and save Python scripts with programs called text editors. There are a plethora of text editors to choose from, including some that work directly in a terminal window (like nano and vi). Even Windows Notepad works as an okay text editor in a pinch.

Another option for creating and editing Python scripts is an integrated development environment (IDE). These are programs designed specifically for software development. They include many useful features such as a text editor, a debugger, and a terminal window. IDEs can even help you write better code by giving you autocomplete suggestions and helping you adhere to code style guidelines. Visual Studio Code (VSCode) and PyCharm are two commonly used IDEs for writing Python scripts.

If you want a deeper dive on the software used to write Python scripts, we have articles on Python text editors and Python IDEs. Be sure to check them out!

How Are Python Scripts Executed?

Python scripts are executed linearly, in a top-down fashion. The script starts executing from the first line and then moves down one line at a time until it reaches the last line and stops. To see the results of any changes you make, you need to re-execute the whole file – even if you only altered a single line.

After writing and saving a Python script, you can run it by executing the python command on your terminal. For example, in order to run the script my_script.py, you would type:

python my_script.py

The command above assumes that your terminal is currently inside the folder where the my_script.py file is located. If you’re unsure of how to do this, take a look at this article about how to navigate around a terminal.

If you’re using an IDE, there’s probably a button in the interface that allows you to execute the Python script directly. Additionally, depending on your Python installation and operating system, you may be able to execute a Python script by simply double-clicking the script file.

What Are Jupyter Notebooks Used For?

Jupyter Notebooks are a bit more complex than regular Python scripts. A Jupyter Notebook consists of multiple cells. Each cell can contain either a block of Python code or plain text (just like in a regular document). This means that you can surround bits of code with useful information, like explanations, links, and images. This makes it easier for you and your colleagues to refer back to the code and understand what it is doing. Output, such as printed messages or plots, appears under each cell in the Notebook.

Python Scripts vs. Jupyter Notebooks

Jupyter Notebooks allow you to mix Python code blocks with formatted text.

Unlike Python scripts, Jupyter Notebooks are executed in a nonlinear fashion. This means that you can execute code blocks in an arbitrary order instead of being forced to execute every block from the top to the bottom.

While this nonlinearity may lead to some confusion if you run lots of blocks out of order, it also means that you don’t have to re-run previous parts of the code after each small change. This can be very helpful if, for example, your code spends a long time loading data from a database. While a regular Python script has to reload the entire dataset on each execution, a Jupyter Notebook lets you run the cell responsible for loading the data once. You can then create new cells to work with the data without ever needing to reload it.

By the way: If you want to learn how to load data from different kinds of sources, have a go at our Data Processing with Python track!

The iterative and exploratory nature of Jupyter Notebooks has turned them into a staple in the data science field. In fact, many data scientists prefer to work with Jupyter Notebooks because it’s easier to test ideas, create prototypes, and share results with colleagues.

How Do You Use a Jupyter Notebook?

Jupyter Notebooks are stored in notebook files, which have the .ipynb extension. Unlike Python scripts, these files are not meant to be directly opened with text editors. Instead, you need to serve these files from a Jupyter server and interact with them through a web browser interface using software like Chrome or Firefox. This is true even if you are editing local Notebooks, without accessing anything over the internet. If this is all going over your head, don’t worry! We have an article on how to install and run Jupyter Notebooks ready for you.

For all of their features, Jupyter Notebooks are somewhat harder to set up than Python scripts. First, you need to install the jupyter-notebook package in your Python environment. Although you can use pip to install it, most data scientists use anaconda, a Python installation that comes bundled with lots of useful data science packages.

Additionally, the fact that you need to initialize a Jupyter server in order to access Jupyter Notebooks – even if you simply want to create and edit them on your own local computer – can be confusing at first.

Pros and Cons of Python Scripts and Jupyter Notebooks

In the previous sections, we briefly touched upon some of the advantages and disadvantages of Python scripts and Jupyter Notebooks. Here is a detailed list of the pros and cons of each solution.

Python scripts

Pros:

  • Scripts are reliable and the most common way to write Python code.
  • Minimal setup is required (you only need a text editor).
  • Top-down execution makes it less confusing to debug and reason through the code.
  • There are many text editors and IDEs with tons of features to choose from.
  • Scripts support modularity. Variables and functions inside a Python script can be imported from another script.

Cons:

  • Scripts must be re-executed to test any changes to the code.
  • Scripts are plain text files. Formatted text or figures cannot be added to them.
  • By default, no output is saved anywhere. The script must be re-executed to see messages, outputs, and results.

Jupyter Notebooks

Pros:

  • Code blocks can be surrounded by helpful notes, figures, and links.
  • Notebooks provide nonlinear execution. Code cells can be run independently from one another.
  • Output (such as messages, plots, and dataframes) appear automatically under each cell, and look great out-of-the-box.
  • Notebooks are good for prototyping data analyses and sharing results with colleagues.

Cons:

  • Notebooks require installing the jupyter-notebook package into your Python environment.
  • Notebooks must be served and accessed through a web browser, making them slightly harder to use than scripts.
  • Nonlinear execution can make debugging confusing, especially if you lose track of which cells you have executed or not.
  • Sharing code or data across Notebooks is not straightforward.

Python Scripts and Jupyter Notebooks – Which Should You Use?

We have examined the differences between Python scripts and Jupyter Notebooks, but one question remains: Which is better? Well, it depends on what you are trying to accomplish.

Jupyter Notebooks truly shine when you are analyzing data and testing new ideas as a data scientist. You can load the data once in the first code cell, then test a bunch of different analyses in other cells. Once you are happy with your discoveries, you can add informative text and figures to the Notebook and share it with colleagues.

Python scripts, on the other hand, are not as iterative as Jupyter Notebooks. They are suited for code that is less dynamic in nature, going over its lines in a predictable top-down fashion. You want to use Python scripts for code that is less about exploring or testing, and more about writing fixed routines or pipelines that may be imported in other scripts.

A nice compromise between Python scripts and Jupyter Notebooks is to start out with a Notebook, using it to explore ideas and get a clearer picture of what you need to do. As your ideas solidify, you can copy the pieces of code in each cell to a Python script, which will hold the “final” version of the analysis.

That’s it for this article – we hope you got a good understanding of the advantages and disadvantages of Python scripts and Jupyter Notebooks! If you need more resources to kickstart your Python studies, why not read about the most popular Python libraries or having a go at our Learn Programming with Python track? Happy learning!