Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Running Python Scripts
A Python script or program is a file containing executable Python code. Being able to run Python scripts and code is probably the most important skill that you need as a Python developer. By running your code, you’ll know if it works as planned. You’ll also be able to test and debug the code to fix errors and issues. Ultimately, you write code in order to run it and accomplish tasks.
In this tutorial, you’ll learn about some of the techniques for running Python scripts and code. The technique that you’ll use in each situation will depend on your environment, platform, needs, and skills.
In this tutorial, you’ll learn how to:
- Run Python scripts from your operating system’s command line or terminal
- Execute Python code and scripts in interactive mode using the standard REPL
- Use your favorite IDE or code editor to run your Python scripts
- Fire up your scripts and programs from your operating system’s file manager
To get the most out of this tutorial, you should know the basics of working with your operating system’s terminal and file manager. It’d also be beneficial for you to be familiar with a Python-friendly IDE or code editor and with the standard Python REPL (Read-Eval-Print Loop).
Free Download: Get a sample chapter from Python Tricks: The Book that shows you Python’s best practices with simple examples you can apply instantly to write more beautiful + Pythonic code.
Take the Quiz: Test your knowledge with our interactive “How to Run Your Python Scripts” quiz. Upon completion you will receive a score so you can track your learning progress over time:
What Scripts and Modules Are
In computing, the term script refers to a text file containing a logical sequence of orders that you can run to accomplish a specific task. These orders are typically expressed in a scripting language, which is a programming language that allows you to manipulate, customize, and automate tasks.
Scripting languages are usually interpreted at runtime rather than compiled. So, scripts are typically run by some kind of interpreter, which is responsible for executing each order in a sequence.
Python is an interpreted language. Because of that, Python programs are commonly called scripts. However, this terminology isn’t completely accurate because Python programs can be way more complex than a simple, sequential script.
In general, a file containing executable Python code is called a script—or an entry-point script in more complex applications—which is a common term for a top-level program. On the other hand, a file containing Python code that’s designed to be imported and used from another Python file is called a module.
So, the main difference between a module and a script is that modules store importable code while scripts hold executable code.
Note: Importable code is code that defines something but doesn’t perform a specific action. Some examples include function and class definitions. In contrast, executable code is code that performs specific actions. Some examples include function calls, loops, and conditionals.
In the following sections, you’ll learn how to run Python scripts, programs, and code in general. To kick things off, you’ll start by learning how to run them from your operating system’s command line or terminal.
How to Run Python Scripts From the Command Line
In Python programming, you’ll write programs in plain text files. By convention, files containing Python code use the .py extension, and there’s no distinction between scripts or executable programs and modules. All of them will use the same extension.
Note: On Windows systems, the extension can also be .pyw for those applications that should use the pythonw.exe launcher.
To create a Python script, you can use any Python-friendly code editor or IDE (integrated development environment). To keep moving forward in this tutorial, you’ll need to create a basic script, so fire up your favorite text editor and create a new hello.py file containing the following code:
# hello.py
print("Hello, World!")
This is the classic "Hello, World!" program in Python. The executable code consists of a call to the built-in print() function that displays the "Hello, World!" message on your screen.
With this small program ready, you’re ready to learn different ways to run it. You’ll start by running the program from your command line, which is arguably the most commonly used approach to running scripts.
Using the python Command
To run Python scripts with the python command, you need to open a command-line window and type in the word python followed by the path to your target script:
After you press Enter, you’ll see the phrase Hello, World! on your screen. If the previous command doesn’t work right, then you may need to check if Python is in your system’s PATH. You can also check where you saved hello.py.
Note: In some Linux distributions and probably in some macOS versions, you may need to use the python3 command instead of simply python.
That’s it! You’ve run your first script! Note that on Windows, you also have the option of using the py command, which triggers the py.exe launcher for console applications. This is the most basic and practical way to run Python scripts.
Note: If you’ve never worked with the command line or terminal, then you can try the following, depending on your operating system: