How to Run Python Scripts on All Major Operating Systems in 2024

Python is a powerful programming language that can automate many everyday business tasks. Use this guide to learn how to create and run Python scripts quickly.

By: R. Paulo Delgado
January 1, 2024
13 minute reading

Python scripts can automate countless repetitive business tasks, saving time and resources. Python scripts can also perform tasks that usually require paid software, such as converting video files to audio files or various AI-related tasks.

Python has extensive support for AI and machine learning, with many built-in and third-party libraries for data processing, natural language processing, and image processing. It’s even possible to train ChatGPT on your business data using a simple Python script. 

However, writing Python scripts requires some programming knowledge, and executing a Python script on your chosen operating system needs some initial steps that can feel overwhelming for non-programmers. 

In this comprehensive guide, we’ll walk you through the necessary steps to create and run Python scripts and show you how to run them on your preferred operating system.

Understanding the Python interpreter and runtime

Similar to Java, Python code runs on top of a runtime—a collection of software and libraries that translates the code you write into a form your computer can understand and execute while managing necessary resources like memory.

Languages that use a runtime let programmers write code in a single codebase without worrying about the underlying hardware.

The Python interpreter is the specific software component of the Python runtime that “reads” the Python code, line by line, and translates it so it can run on the underlying device.

To get Python to run on any device, you only need to ask, “Does a Python runtime exist for that specific operating system that converts regular Python code into the underlying machine code in real time?”

If the answer is yes, then you can run Python scripts on that machine. 

Several official Python runtimes exist, covering the major desktop operating systems: Linux, Windows, and macOS. Some alternative Python versions also exist for other operating systems. 

The first step to running Python scripts is to install Python using the version for your operating system. 

If no Python runtime exists for the operating system you’re targeting, it’s unlikely that you’ll be able to run Python scripts on that operating system. 

Install and configure the Python runtime and interpreter

To install Python, navigate to the Python download page and select the version of Python that matches your operating system. 

After installing Python, you might need to configure your computer’s PATH variable.

The PATH variable contains a list of directory paths in Linux, Windows, and MacOS operating systems. When you type a command into the terminal (in Linux and MacOS) or the command prompt (in Windows), the operating system searches these directories in the order they are listed in PATH to find the executable file for the command.

To check if you need to manually configure the PATH variable, open a terminal window or command prompt, type in the word “python” in lowercase, and then hit Enter. On Linux and macOS, you might need to type “python3.”

On Windows, open the start menu and search for “cmd.” Then, open the Command Prompt app by clicking on it. 

On Windows, search for “cmd.”

On Windows, search for “cmd.”

On MacOS, search for “terminal” using Spotlight search. 

You can open the terminal on many Linux distributions by using Ctrl + Alt + T. 

Type “python” into the command prompt/terminal. If the PATH variable is set correctly, you’ll see something similar to the following on your output screen: 

Python via the command prompt.

Python via the command prompt.

The three angle brackets (chevrons) indicate that you’re interacting directly with the Python interpreter. You can type any Python command after those chevrons, such as 'print(“Hello, Fiverr”),' and the Python interpreter will run it for you when you press enter. 

Python “Hello, Fiverr” command via the command prompt.

Python “Hello, Fiverr” command via the command prompt.

If Python isn’t installed, or the PATH variable isn’t properly configured, you’ll receive a message similar to:

Python command not found on Linux terminal.

Python command not found on Linux terminal.

Setting the PATH variable on Windows

We need to find where the python.exe file is installed. To do that, open Windows search by clicking the Windows key and S. 

Search for “python.exe.” Right-click the python.exe file that appears, and click “Open File Location.”

Search for python.exe in Windows.

Search for python.exe in Windows.

Then, copy the address bar location for this file. 

The location of the python.exe file. 

The location of the python.exe file. 

To add that directory path to the Windows PATH variable, press the Windows key and S, and search for “Environment.”

Click “Edit the system environment variables.”

Search for “environment” in Windows search. 

Search for “environment” in Windows search. 

  • Click “Environment Variables” on the next screen that opens.

  • Find the PATH variable and click “Edit.”

  • Click “New” and paste the directory path you copied into the new value. 

Return to the command prompt you opened earlier, type “python,” and press Enter. You should now see the three chevrons and data on what Python version you’re running. 

Adding the Python path to Linux and macOS

Updating the path variable manually in Linux and macOS usually isn’t necessary. Python often comes pre-installed on many Linux distributions. 

If you get an error when typing “python” into the terminal, the simplest solution is to reinstall Python. 

If you’re still having trouble updating the PATH variable manually on Linux, macOS, or Windows, you can buy support and IT services from Fiverr freelancers to help you. 

What is the difference between Python code, scripts, modules, and apps?

“Code” refers to the words and text of any programming language. Computer programmers are sometimes called coders, and programming is also called coding

What are scripts?

Scripts are files of code that rarely need to be compiled for someone to use the script. Scripts are useful for IT administrators and power users because you don’t need to run the code through a compiler. 

A power user can write a script in a text editor and execute it directly on their machine. If the script runs into an error, that script’s interpreter outputs the line that caused the error, and the user can modify the script quickly and then run it again. 

Compiled languages, such as C++, are more opaque. You can’t simply create a single C++ script file and then run it. You have to compile that file into machine code and then run it, which slows the process down.

Compiling code also obscures its contents because the final executable file is in binary form. IT admins and power users typically accumulate a large collection of scripts to automate repetitive tasks. Compiling these scripts makes it challenging to understand what each one is doing if the user needs to use it again a year after creating it. 

To know what a script file is supposed to do, you only need to open the file in a text editor and read the code. This isn’t possible with compiled executables. 

Most code editors, such as Visual Studio Code or Notepad++, highlight the script file’s syntax, making it even easier to understand what the script file is supposed to do.

The following code in Notepad++ shows syntax highlighting for a simple Python script. 

Syntax highlighting in Notepad++.

Syntax highlighting in Notepad++.

Python’s simple syntax and its use as a scripting language mean that IT admins are more likely to choose Python over C++ for building simple utilities. 

Perl was once a prevalent scripting language preferred by many IT admins. Since Python matured into the powerful language it is today, many IT professionals now prefer Python over Perl for scripting tasks.

At its core, each script file is really just a text file. On Windows, the script file’s extension tells the system how it should be treated. For example, Python script files end in .py, and Windows associates the respective app to deal with that file. 

The file extension is mostly meaningless on Unix-like systems, such as Linux. The Unix system reads the first line of the file’s contents to know what app it should use to run that file. 

The first line of an executable script file on a Unix system has a “shebang” to tell the system how to treat that file. 

A shebang is the following sequence of characters: #!

After the shebang, you type the path to the interpreter you’d like the system to use to interpret that script file. So, if you’d like to write a bash script on a Unix system, you include the following command as the first line of the script file:

#!/bin/bash

Regardless of the file’s extension, the Unix-like system would interpret that file as a bash script. 

Modules

Python modules and scripts are both text files that contain Python code. The difference lies in their intended use. 

Python scripts are typically standalone code files that users can execute directly. Scripts have an entry point. 

Modules are Python code files that get imported using the import keyword and rarely have an entry point, so you can’t run them directly because the interpreter doesn’t know where to start processing that file. 

When you import a Python module, the importing script executes the code in the module. 

Using modules allows programmers to separate the logic of their Python projects into categories, making it easier to maintain the code in the future. 

A script file can import additional Python modules. 

For example, let’s imagine we have a script file called find-fiverr-freelancers.py with the following Python code:

Python code demonstrating importing Python modules.

Python code demonstrating importing Python modules.

Separately, we have four other Python files:

  1. python_programmers.py

  2. ai_programmers.py

  3. website_designer.py

  4. search_fiverr.py

Because we’re importing these files using the import keyword, we call them Python modules instead of Python scripts. 

When importing modules in Python, you don’t need to add the .py file extension of that module in the source code. 

Python apps

A Python app is a set of modules, at least one script that defines an entry point, and other resources that work together to create a fully functioning app. The additional resources might include GUI (graphics user interface) elements, images, HTML files, and external libraries written in other programming languages that work together to create a standalone application. 

To learn more about how Python scripts and modules work together, you can buy online coding lessons from Fiverr freelancers to become an expert Python programmer. 

The Python interpreter runs Python scripts

Python scripts end with a .py extension. So, a Python script that counts from one to 10 might have the file name counter.py

To run the counter.py script, you’ll need the Python interpreter to read and execute that .py file. 

Various ways exist for the Python interpreter to execute a Python file directly, such as: 

  • Calling the Python interpreter from a command prompt or terminal 

  • Clicking the Run button in your preferred IDE (integrated development environment) 

  • Using a Python REPL (Read-Eval-Print Loop) tool that lets you run Python interactively 

Let’s dive into the details of how each of these is done. 

Writing your Python script

Python is a beginner-friendly programming language. It’s easy to read, has straightforward syntax, and abstracts many low-level tasks away from the programmer, so you can focus on writing task-oriented code while the Python runtime takes care of the rest. 

If you have no experience writing Python code, you can use ChatGPT to write code for you or buy Python programming services from Fiverr experts to create useful Python scripts. You can also buy online coding lessons from Fiverr freelancers to help you tweak the scripts that ChatGPT produces for you. 

How to run Python scripts from the command line

  1. Open a command prompt or terminal.

  2. Type “python” or “python3,” followed by the path to the script you want to run. 

  3. If the path contains spaces, enclose it in quotation marks.

For example:

python “path/to/your/python/script.py”

Command-line arguments

You can add powerful functionality to your Python scripts by including command-line arguments. 

For example, imagine you have a script that periodically checks a client’s website and logs its status every few seconds. At the end of each day, you can send this log to your client to report on how much downtime the site had. 

You could hardcode the website’s address into your script and create a different script for every client you have. Unfortunately, that means creating many additional script files that do the same thing, making maintenance a problem. 

Alternatively, you could pass the website addresses as an argument to the script. Passing the addresses as an argument means you must only maintain a single Python script file, regardless of how many clients you obtain. 

To read command-line arguments in a Python file, use Python’s built-in sys module and the argv array. 

The following code demonstrates how to access a named argument, such as “--websites.”

Python code showing how to use sys.argv with a named argument.

Python code showing how to use sys.argv with a named argument.

You can then call this script from the command line as follows:

python your-script.py --websites “https://www.example.com/, https://fiverr.com/, https://google.com”

How to run Python scripts by double-clicking the Python file

As helpful as running Python scripts from the command line is, it becomes cumbersome when you have scripts in many different locations. 

Several methods exist to make a script executable through a double-click in your computer’s file manager, depending on your operating system. 

These methods are:

  • Windows:

    • Associate .py files with python.exe (not recommended). 

    • Create a batch file that calls the script, then double-click the batch file. 

  • Linux:

    • Change the file’s permission so it’s executable using the “chmod” command and add the Python shebang at the top of the file. 

  • macOS:

    • Running scripts directly on a Mac is most easily done by wrapping the script in a launcher like Platypus

How to run Python scripts directly in Windows

We don’t recommend associating .py files with python.exe because you might want to edit Python files frequently by double-clicking them. If you associate the files with python.exe, you’ll always have to right-click the file to edit, which can become frustrating. 

The preferred way to run a Python script directly in Windows is to wrap it in a batch file. 

To do this:

  1. Create your Python script. 

  2. Open a text editor and type the following command: python “C:\path\to\your\python-file.py,” replacing “C:\path\to\your\python-file.py” with the path of the file you created in Step 1. 

  3. Save your file with a .bat extension, such as “my-python-executor.bat.”

  4. To run the Python script, double-click the my-python-executor.bat file. 

You can also add command-line arguments to your batch file by adding them in the batch file after your script’s file path, as follows:

python “C:\path\to\your\python-file.py” argument1 argument2 argument3 etc

If you need more help to create your wrapper batch file, you can buy IT support services from Fiverr freelancers to help you. 

How to run Python scripts directly in Linux

Most modern Linux distributions impose restrictions on scripts, so you have to right-click them and select “Run as program” to execute them directly. Although it’s technically possible to change the default behavior so that double-clicks execute the script directly, we recommend avoiding this for security reasons. 

To make a Python script executable on Linux, do the following:

  1. Open a terminal window. 

  2. Change the Python script’s permissions so it’s executable by using the command: chmod +d “path-to-your-python-file.py”

  3. Close the terminal window. 

  4. Add a shebang line to the top of your Python script. It should say exactly: #!/usr/bin/env python3.

  5. Right-click the Python file and select “Run as program.”

Note: On Linux, adding “.py” to the end of your script file’s name is unnecessary. 

How to run Python scripts directly on a Mac

Because of macOS’s security settings, we recommend wrapping your Python script in a tool like Platypus. Other ways exist to make a Python script executable on Mac, but this is the simplest and most user-friendly. 

After installing Platypus, you can use its user interface to turn your Python script into a macOS executable app. 

How to run Python scripts on Android

No official Python runtime exists for Android, but you can install QPython, which implements a Python interpreter, so you can run Python scripts and apps on Android. QPython has extensive support for most standard Python libraries and many third-party libraries. 

How to run Python scripts on iOS

No method exists to run Python scripts directly on iOS devices. However, you can install Pythonista, a Python IDE, from the app store. The IDE includes support for Python 3.5, and you can run scripts from inside the IDE. 

Running Python scripts using an IDE (integrated development environment)

One of the simplest ways to run Python scripts on any mainstream operating system is to run it directly inside an IDE, such as PyCharm, IDLE, or Visual Studio Code

Running Python scripts this way lets you test and run the script iteratively until you get it performing exactly as you want. 

Running Python scripts in Visual Studio Code

To execute a Python script in Visual Studio Code, open your script, then press F5. If you’re missing any extensions to run the script, Visual Studio Code will prompt you to install them. 

You can also set breakpoints in the script for Visual Studio to pause execution, allowing you to inspect variable values and simplify debugging. 

Breakpoints in Visual Studio Code let you debug Python scripts easily. 

Breakpoints in Visual Studio Code let you debug Python scripts easily. 

Running Python scripts in IDLE

Python ships with its own simple development environment called IDLE, which stands for Integrated DeveLopment Environment. 

IDLE includes a Python Shell to run Python scripts in interactive mode. You can type Python commands directly into the Python shell and test code line by line. This type of testing is called REPL—Read-Eval-Print Loop, which means the shell reads the code, evaluates it, prints any output, and then waits for the next input (loop). 

Running Python interactively in IDLE.

Running Python interactively in IDLE.

IDLE is a minimalist tool and lacks many of the sophisticated features of other IDEs, such as syntax highlighting and autocompletion, which can slow development time down. 

However, it’s an excellent, lightweight tool for testing and learning Python code. And it’s straightforward to run Python scripts with IDLE. 

To run Python scripts in IDLE:

  1. Click “File” in the top menu. 

  2. Click “Open.” 

  3. Select the script you want to run. 

  4. Click “Run” and then “Run Module,” or press F5 in the new script window. 

Hire Python programmers on Fiverr

Fiverr is a marketplace of expert freelancers skilled in everything from website and logo design to Python programming and AI chatbot development. 

Fiverr freelancers offer all the major services that small businesses need to compete successfully with larger enterprises. 

Freelancers are ranked according to their experience on the platform and customer satisfaction rating. Fiverr’s safety and security team ensures that the buyer and seller experiences are excellent. 

To find the right Fiverr freelancer for your project, open an account on Fiverr and search for the service you need, such as “Python programming” or “Python AI developer.” Look through the gigs that appear, and contact any freelancers you’d like to learn more about. You can either buy services directly or ask the freelancer to send you a custom offer. 

To get started, sign up for Fiverr today.

About Author

R. Paulo Delgado Tech & Business Writer

R. Paulo Delgado is a tech and business freelance writer with nearly 17 years of software development experience under his belt, including WordPress programming. He is also a crypto journalist for Moneyweb, and proudly a member of Fiverr's Pro Seller program — hand-vetted professionals, verified by Fiverr for quality and service.