How to Run Python Programs/Scripts

Written by Peter Steele

April 25, 2023

Categories: Python
How to run Python Scripts/Programs

Introduction

So you have finally creatd the perfect script or just downloaded a really cool program built in Python but aren’t quite sure how to run it? Well in this tutorial we are going to cover how to run Python scripts and programs on various different operating systems! Strap in, this is a fun one and much easier than you think!

Prerequisites

If you haven’t already, you need to make sure that you have installed Python to your system or that Python is already installed be default on your system. If you are unsure of how to do this, please check out one of our guides on how to do so: Install Python on Ubuntu or Install Python on Windows. MacOS and Linux usually already come with Python installed by default, and these articles will let you know how to check to see what version you are running and to make sure your terminal or command prompt has access to the Python command already.

Windows

Command Line

So the first one up today is how to run these scripts from the Windoes command line. This is going to be most similar to other operating systems like Linux and MacOS. To begin first open up a command prompt by searching in the Windows Search Bar, or by pressing Windows Key + R and typing in cmd.exe

Once you have the command prompt open, simply navigate to the folder that contains the script you are looking to run with the cd command.

cd (Change Directory) to your Python Files

cd path/to/your/files

Once you have gotten to the location that houses your files that end in .py, then to run those scripts, all you need to do is simple type out the following.

Run the Python Script/Program

python some_script_name_here.py
python path/to/script/some_script_name_here.py

Now of course the python prefix can vary, if you followed my tutorial on how to Install Python on Windows, then you will know all about the py launcher feature that lets you specify different versions of python easily like so:

How to Use PY Launcher to Run Multiple Versions of Python

py -2 some_script_name_here.py
py -3 some_script_name_here.py
py -3.10 some_script_name_here.py

So that is about it when it comes to running Python scripts in the Windows Command Line. This is basically the same process for Linux and MacOS, though I don’t believe those OS’s have the py luancher feature built in, but we will cover that in those sections.

NOTE: You do NOT have to navigate to the script directly to execute it, you can provide the path to the script from whatever directory you are in without needing to navigate there directly.

Windows GUI

To run Python scripts from the Windoes GUI using your mouse, it is as simple as double clicking on the script or program files that you wish to run. This will then open a popup window asking you how you would like to open this file. Select the Python option and the script or program should start running. You can choose Always or Just Once. If you choose Always, it will always use Python to open your .py files and you will never have to mess around with that again. If you choose Just Once, you will need to select this option each time. This is available on Windows 11. Older versions of Windows have a different way to handle this that I explain later on.

Run Python Script by Double Clicking

Be aware, that they may flash the command prompt briefly and vanish, this is because there is no pause or break before the end of the program execution to keep the window open. If this is a script that you have built yourself you may need to put in a break or pause at the end of the file in order to keep the window open if it is a console based application.

If this is launching a GUI like Tkinter or something else, then the program should just launch like normal Windows applications without issue.

Setting the Default Program for Older Versions of Windows

As I mentioned before, older versions of Windows don’t have the Just Once and Always options. You can also set the default program to handle opening all .py files on the system if you do not want to have to select Python each time. To do this, simply navigate to any Python file in your system:

right-click on that script-> choose open with-> choose default program-> more options-> and select the python.exe option.

Creating a Batch Script

Ok, so you want something a little more robust that can maybe run multiple scripts at once and that you can just double click on and it runs without having to deal with selecting Python each time to run the scipt or program. Well batch scripting is the best way to handle this in Windows and allows you to do some really powerful stuff! We will be creating an entire series on batch scripting in the future, but for now lets look at the basics of setting up a simple script to handle running your python scripts.

Creating a Batch Script to Run Python Files on Windows

@echo off

rem Run first Python script
echo Running first Python script...
python C:\path\to\first_script.py

rem Run second Python script
echo Running second Python script...
python C:\path\to\second_script.py

rem Run third Python script
echo Running third Python script...
python C:\path\to\third_script.py

rem End of script
echo All scripts have been run.
pause # Used to pause the Console/Terminal so you can see the output of the program

In this script, the @echo off command turns off the echoing of commands in the console window. Then, the first Python script is run using the python command and the path to the script file. This is repeated for the second and third scripts. Finally, the script outputs a message that all scripts have been run and pauses to wait for the user to press a key before closing the console window.

As you can see this is rather simple to do and is very powerful allowing you to run multiple Python scripts in a row. Just to clarify, the rem in this script is for remarks and allows you to add comments to the script. If you wish to see the code in the console, such as running a script or program that is a console app, make sure to exclude the @echo off line at the top of the script. Next lets cover how to do this in Linux!

Linux

Terminal

Ok so much of what we covered in the Windows section also applies to Linux and by extension to macOS, with slight differences. In Linux, you can open the python scripts or programs in the terminal app by also typing out

Run Python in the Linux Terminal

python path/to/file/some_script_name.py
python script_name.py

I have provided two different ways, which can also be done in Windows. One lets you set the full path to the file you wish to run from whatever directory you are currently in without having to navigate to it, and the other allows you to run a script in the directory you are currently in. You can do the same on Windows, and just like how I explained in the Windows section, you can cd to the location of your script directly instead.

Linux GUI

To double click and run a Python script or program from the Linux GUI you will need to make the script or program executable. To do this, you will need to go into the Terminal app and run a simple command against the file in question. Once you have done that, you will be able to double click and run the file.

Make Python Script Executable

chmod +x script_name.py

Next, you will want to make sure that the Python script or program that you are wanting to run has this shebang at the top of the file so the system knows what interpreter to run:

Add Shebang/Hashbang to Python Script

#!/usr/bin/env python3

At this point, you should be able to double click on the file in the GUI and have it run or just run it from the terminal app by specifying its name only.

Run Executable Python Script

script_name.py

Another additional step that you can take is to move the script or program file to a directory that is included in your system PATH, so that you can run the script from anywhere on the system without needing to type out the full path to the script or navigate to its directory directly.

Move Script to Directory in System Path

sudo mv script_name.py /usr/local/bin/

Special Note

Remember earlier I mentioned the py launcher that was available on Windows. This is a Windows specific tool to allow switching versions of Python easily. This tool is not available on Linux or MacOS so you will either need to type out the full path to the Python interpreter that you want to use, set up Symlinks, or use something like Python2 or Python3 in order to run the scripts. To setup a Symlink, it is rather straight forward:

How to Symbolic Link Python Versions to Different Commands

sudo ln -s /usr/bin/python3.9 /usr/local/bin/python3.9 # name of the command you will call
sudo ln -s /usr/bin/python2.7 /usr/local/bin/python2.7

# How to call the new symlinks
python3.9 script.py 
python2.7 script.py

So as you can see, the command is rather simple, but allows you to create any name that you wish to use to call various versions of Python in Linux and MacOS. The name you specify at the end of the /usr/local/bin/ is the name of the command that you will use to call that version of Python. Make sure the paths to the versions of Python you are symlinking are correct or you will not be able to run the scripts or programs that you wish.

Linux Shell/Bash Script

So just like on Windows, you can easily create shell/bash scripts to run multiple Python scripts or programs at once. The term Bash or Shell are often used interchangeably in Linux as they refer to the Bash shell. There are other shells available on Linux, but this is the most common one that you will encounter. To create the shell script, you just need to do a few simple steps:

Create a Shell/Bash Script File and Run It

You can use something like Nano, Vim, or Gedit in order to create the shell script file that will house the code to run your Python script or program.

Create and Open a New Shell Script

nano runscript.sh # Creates a new file and opens it in Nano
vim runscript.sh # Creates a new file and opens it in Vim
gedit runscript.sh # Creates a new file and opens it in Gedit

Code to Include in Shell Script

#!/bin/bash
python myscript.py # Only works if the Shell Script and Python Script are in the same directoy
python /path/to/script/myscript.py # Best to put the full path if you want to run the script from anywhere

Make the Shell Script Executable and Run It

chmod +x runscript.sh # Make the script executable
./runscript.sh # Run the script

So as you can see, it is very similar to the Windows batch script with just a couple extra steps in that you need to mark the script as executable and then run it. You should also be able to just double click the script now that it is executable from the GUI and have it run that way as well. Also be aware that this functionality is defined by the file manager that you are running. While most Linux file managers support double clicking and running shell scripts that have already been marked as executable, not all do. If you run into any issues, google your file manager that you are using to see if it supports this feature or not, and how to enable it if it is not enabled by default. I would cover how to do that here, but there are so many different ones, it would be impossible to list them all.

In the next section we will cover MacOS and wrap this tutorial up!

MacOS

Terminal

So very similar to Linux, you are just going to type out:

Running Python Script on MacOS Terminal

python script_name.py
python path/to/script/script_name.py

Nothing needs to change here as both Linux and MacOS are based off of Unix and share many of the same commands and ways to run things. 

MacOS GUI

So unlike Linux there may be some additional steps to allow double clicking and running a Python script on MacOS. 

The general idea is the same, you first need to chmod +x the script and then double click on it. MacOS however sometimes will prevent you from running unsigned programs. Lets recap making the script executable and then I will talk about how to get around gatekeeper on Mac to allow you to run the script or program.

Make Python Script executable on MacOS

chmod +x script_name.py
chmod +x path/to/script/script_name.py

Ok, so that covers marking the script as executable, now you can try double clicking on it and see if it will run. If not, Gatekeeper is preventing unsigned programs from running on your system and you need to bypass it.

WARNING: this is not something to do lightly and only do it on software and scripts that you trust. 

These are the steps you will need to take in order to grant permission for your script or program to run:

  1. Open System Preferences.
  2. Click on Security & Privacy.
  3. Click on the General tab.
  4. Under “Allow apps downloaded from,” select “App Store and identified developers” or “Anywhere” (if it’s available).

Note: “Anywhere” is not available by default on newer versions of macOS, but you can enable it by running the following command in the terminal:

Disable Gatekeeper on MacOS

sudo spctl --master-disable

This will disable Gatekeeper, which enforces code signing and verification of downloaded apps.

WARNING: Disabling Gatekeeper can increase the security risks on your system. Make sure you only run trusted software from trusted sources.

This warning is worth repeating as it can be dangerous if you run scripts or software you did not write yourself and open youself up to malware and other attacks.

MacOS Shell Script

Same as the Linux shell script you can also create these on MacOS to run mutliple Python files at once or just have an easy way to execute a script with some CLI parameters that you may not want to type out everytime. Ill show a quick example below but it is the same process as on Linux. 

Create MacOS Shell Script

vim myscript.sh

Add Code to Shell Script with CLI Parameters - MacOS

#!/bin/bash

python3 /path/to/your/python/script.py "$1" "$2"

Mark Shell Script Executable and Run It

chmod +x myscript.sh
./myscript.sh arg1 arg2

So this script can except some CLI parameters that can then passed into the python script or program each time it is ran. You can also double click the shell script and run it if you have permission or if you have disabled Gatekeeper to run unsigned code. 

That about covers the most popular ways to run python scripts or programs in MacOS, lets wrap this tutorial up below.

Conclusion

So that about wraps up the various ways that you can run Python scripts on various OS’s. As you can see, there are several ways to acheive the same results and whichever you choose is honestly down to personal preference. Windows py launcher is really a handy tool that makes working with Python an enjoyable experience, which was not always the case on Windows in years past. With the power of Symlinks on Linux and MacOS, that is honestly the easiest route to take. Of course you can create shell scripts as well, just like creating a batch script on Windows to handle running more than one Python script or program at a time.

We hope you enjoyed this tutorial and learned something new today. As always, feel free to look around at our ever expanding set of tutorials and leave us a comment and share with your friends if you found this content helpful!

Related Articles

Why Python?

Why Python?

Introduction Python is one of the most popular programming languages in the world, with a vibrant community of developers and a wide range of applications. It's also an excellent language for beginners who are just starting to learn programming. In this blog post,...

Installing Python on Windows 10/11

Installing Python on Windows 10/11

In this tutorial, we will cover how to properly install Python on Windows 10/11 and how to make sure Python is included in your path. We also cover how to use Python with it’s IDLE and inside of the command prompt.

Python Variable Names

Python Variable Names

Introduction   Python is a powerful programming language, and here at Learn Code Today, it is also one of our favorite to work with! Over the years, Python has jumped to the top of the pack for programming languages over competitors because of its versatility and...

5 1 vote
Article Rating
Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Nick

@echo off is cool! I’ve always just redirected all echos to /dev/null which is a pain.

Peter Steele

Peter Steele

Software Engineer/Site Owner

I am a father of 2, happily married and I also love to game! I started this site as a way to pass on what I have learned as a software engineer. Hopefully I can pass on some knowledge to those seeking it and make someone's life just a little better. Happy coding!

Pin It on Pinterest