Skip to main content

How to Run Python Scripts Tutorial

Learn how you can execute a Python script from the command line, and also how you can provide command line arguments to your script.
· 10 min read

Explore with AI

Run and edit the code from this tutorial online

Run code

Typically, every novice learns to write a Python script on the command line first, then moves to execute the script from the command line, wherein the script is usually written in a text editor and is run from the command line. In this post, we explore in more detail how to run Python scripts. 

Python Scripts and Interpreters

Computers cannot understand code in the way humans write it, and hence, you need an interpreter between the computer and the human-written code. The interpreter's job is to convert the code into a format that computers can then understand and process.

The interpreter processes the code in the following ways:

  • Processes the Python script in a sequence

  • Compiles the code into a byte code format which is a lower-level language understood by the computers.

  • Finally, a Python Virtual Machine (PVM) comes into the picture. The PVM is the runtime powerhouse of Python. It is a process that iterates over the instructions of your low-level bytecode code to run them one by one.

Like scripts, you have something called Module, which is a Python script imported and used in another Python script.

The Python script is saved with a .py extension which informs the computer that it is a Python program script. Unlike Windows, Unix-based operating systems such as Linux and Mac come with Python pre-installed. Also, the way Python scripts are run in Windows and Unix operating systems differ.

Running Python Scripts: Getting Set Up

For all users, especially Windows OS users, it is highly recommended that you install Anaconda, which can be downloaded from the Anaconda website. You can follow along with the instructions given in this installing anaconda for Windows tutorial.

To install Python3 on a Windows operating system, please feel free to check out this Python setup tutorial.

Command-line interpreter for Python can be accessed on the various operating systems in the following ways:

  • On Windows, the command line is known as the command prompt, which can be accessed by clicking the start menu and search for command prompt. Another way is by going to the Run dialog box type cmd and press enter (Start menu $->$ Run and type cmd). After which, if Python is installed in your C-drive then all you have to do is type C:\python36\python.exe, where python36 is the Python version (Note: This path may vary based on the directory at which Python is configured).
command prompt
(Source)
  • On a Mac system, it is very straightforward. All you need to do is open Launchpad and search for Terminal, and in the terminal, type Python , and it will give you an output with the Python version.
mac system
  • Like the Mac system, accessing the terminal on a Linux system is also very easy. Right-click on the desktop and click Terminal in terminal type Python.
linux system