How to Run Python Scripts

Python is one of the most popular programming languages in the world. It is extremely powerful, has easy syntax and plenty of useful libraries that you can use to build projects.

If you’re starting to learn Python, then knowing how to run your Python scripts is an important skill to learn. If you don’t know how to run your scripts, you can’t verify whether they actually work. It is the only way to really test your programs.

In this step-by-step guide, you’ll learn everything you need to know about the basics of running Python scripts. We'll cover how the techniques used for running scripts differs based on your environment, your operating system, and even your skills as a developer.

Table of Contents

You can skip to a specific section of this Python tutorial using the table of contents below:

There are two main types of files in Python: scripts and modules.

Before proceeding further, it is important to highlight the difference between the two. The next section will explore this dichotomy in detail.

Difference Between Scripts and Modules

When you create a new Python file to write a simple program that outputs a “Hello World” statement, then what you’re actually writing is a Python script.

This is a Python file that can be executed directly by the user using any of the techniques mentioned in the preceding Table of Contents.

The script is also known as a top-level program file. It is processed by an interpreter, which parses and executes the file line-by-line in a sequential manner.

A module, on the other hand, is a Python file that is meant to be imported and used by outside Python scripts. If you've ever used NumPy, pandas, matplotlib, or any other open-source Python library, then you've used modules. Libraries are simply collections of modules.

You can indeed run a module by itself. However, that defeats the purpose of creating module files.

The main difference between scrits and modules is that Python scripts are meant to be directly executed, while modules are imported and used by other files (namely scripts).

Knowing the difference between the two will allow you to understand how to effectively organize your Python programs.

Scripts are useful when you’re trying to build a short program. However, if you’re trying to better organize your program and want to write reusable code, then writing modules is better.

A Python script or module relies on an interpreter for its successful execution. If you're not familiar with interpreters, you'll learn about them in the next section of this article.

What’s the Role of a Python Interpreter?

The interpreter basically executes the program from the start to finish and follows the instructions contained in the codebase. Before running scripts and modules, you need to install an interpreter on your system.

Downloading and installing Python from the official website installs the Python interpreter on your system. You can also install the interpreter using Anaconda, which helps in package management.

You can include a Python program in an application written in a completely different language. This is possible with the help of Python interpreters written in different programming languages.

Here’s a list of popular Python interpreters:

All interpreters perform the same task. They execute Python code. Because of this, it's critical that your interpreter is installed correctly on your system.

The rest of this tutorial will explain the many different ways you can run Python code on your computer. I'll start by showing you how to run Python code in an interactive session.

Running Python Interactively

The quickest way to run Python commands is by creating an interactive session on your terminal.

To do this, simply open the command-prompt or terminal on your system and type Python or Python3 (depending on the type of Python you’ve installed).

Here’s how you can creating an interactive session on Windows and run a small program that does the following:

  • Creates two variables, x and y, and assigns them values of 10 and 15
  • Prints the sum of the two variables
  • Prints Hello World
C:\Users\username>Python3
Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 10
>>> y = 15
>>> print(x+y)
25
>>> print("Hello World")
Hello World

Whenever you see >>> in your terminal, you know you’re in interactive mode.

In this mode, you can write and run Python without having to configure anything else.

The downside of using interacting mode is that the terminal is far from the best text editor. More importantly, all your code is lost when you close the session.

The interactive session should be used solely for testing small examples and basic logic. When using the interactive session, each command is evaluated immediately. This characteristic is really helpful if you’re a beginner who needs to verify your understanding of syntax and commands.

You can use other scripts and modules in an interactive session using the import command. With that said, if you're building a script that is large enough to rely on outside code, then you'd be better off using some of the other methods to run Python that we'll discuss later.

To close the interactive session, use the quit() command and the session will end. You can also use the following key combinations to quit:

  • ctrl + Z and then Enter for Windows
  • ctrl + D and then Enter for Linux-based systems

Running Python Using The Command Line

Modern developers generally do not use interactive sessions because they do not store the code and logic after the interactive session has been terminated.

Python files can be created using any plain text editor. You can use the operating system’s native text editor or you can try out VS Code, which is one of my preferred text editors.

The benefit of using a text editor like VS Code is that these editors have optional plugins that can help boost your productivity. Another great feature provided by VS Code is IntelliSense, which is a code-completion tool.

IntelliSense is a controversial project because many develoeprs believe that it hampers the learning of beginners. I believe IntelliSense is absolutely necessary for any serious software projects. It autocompletes code and allows you to check methods.

Let's move on to learning how to run Python from the command line. Consider the following test script for the rest of the tutorial:

#!/usr/bin/env Python3

print('Hello World!')

For Windows-based systems, the first line, which is known as a shebang, is not required.

For Linux-based systems, the shebang helps in identifying the program that will be used for running the file. In this case, this is Python3.

The script simply outputs Hello World. The script's name is helloworld.py. We'll use this as an example for the rest of this tutorial.

Using The Python Command

You can run our helloworld Python script with the following command:

C:\Users\Python> Python helloworld.py
Hello World!

It’s that simple to run the script. The command will execute your script and all the associated modules and write the output in the terminal.

On Windows, if it gives an error such as Python is not recognized as an internal or external command, operable program, or batch file then check your environment variables. This stackoverflow answer has more details.

Storing Output

If you want to store the output of your script for later use, you can use the angular bracket >. The command becomes:

C:\Users\Python> Python helloworld.py > output.txt

This will store all the output in the output.txt file. You can open the output.txt file to view its output, or even interact with it using a different Python script.

Running Python Using VS Code

Serious programming projects require advanced text editors like VS Code or Sublime Text.

Along with the benefits of IntelliSense and external plugins, another advantage of these text editors is that they allow you to run the code using native terminals.

The default Python integrated development environment (IDE) is IDLE. It provides basic text editor functionality and is fast and reliable.

However, IDLE lacks an aesthicaly-pleasing look. It's also missing IntelliSense and other plugins that could improve your productivity.

Using built-in terminals available with modern text editors is convenient when you’re coding and want to quickly test your program's output. You don’t have to switch to a command prompt or terminal and can quickly type in your command to start the program.

Here’s the terminal of VS Code, running the script that we referenced earlier:

How to Run Python Scripts Screenshot

To learn more about how advanced text editors work and how you can enjoy their in-built features, read their documentation.

Running Python From a File Manager

This is by far the simplest way to run a Python script.

In Windows, you can simply double-click the icon of the Python file in your file manager. Since Windows associates file types with their corresponding programs, it will automatically run it using the Python interpreter.

The same trick can be performed on Linux. However, you will have to include the shebang and specify the file execution privileges using chmod.

A quick warning is warranted here. Pay attention if your script is as simple as the one written for this tutorial. For short programs like this, you will probably just see a flash of the command prompt or terminal before the program concludes and the terminal is closed.

A work around this situation is to add a statement in the code that requires a user input. For example, input(“enter variable x”).

This will stop the execution of the program and allow you to see the progress until that command. The program will remain paused until you press Enter.

This approach is not advisable for production applications as it will fail if there are bugs and errors in your program.

Final Thoughts

Python is a fun language to learn. It is easy, powerful and has tons of support on the internet. These characteristics make Python a very programmer-friendly language that a beginner can learn quickly.

The goal of this tutorial is to make you feel comfortable running Python scripts. You've learned the different ways to run Python and the role of interpreter in executing your Python program. This will allow you to improve your development process and efficiently run (and iterate on) code that you've written.

If you enjoyed this article, be sure to join my Developer Monthly newsletter, where I send out the latest news from the world of Python and JavaScript:


Written on June 20th, 2020