How to Run Python Programs/Scripts

How to Run Python Programs/Scripts

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!

Install Python on Ubuntu 22.10

Install Python on Ubuntu 22.10

Introduction

Hello everyone, today we are going to look at installing Python on Ubuntu 22.10, but this will also apply to any version of Ubuntu that uses the apt-get package management system. To get started, you are first going to need to open up your terminal. The shortcut for this is Ctrl + Alt + T. Once the terminal is open then you need to install the version of Python that you would like. 

By default, any newer version of Ubuntu is going to come pre-installed with Python 3. This will usually be one of the latest versions that are available at the time of the Ubuntu release, but it may be even newer if you have already updated your system packages to the lastest and greatest. 

Install default Python 2/3: 

To get started lets assume you are wanting to install an older version of Python like Python 2. 

Install Python Command

sudo apt-get install python2

Once you press enter, you will see a bunch of package names appear that need to be installed along with a prompt asking if you are sure you wish to install these packages. Type in ‘Y’ and hit enter again for the packages to be installed. Congratulations, you have installed python to your system!

Now if you accidentally deleted Python 3 or corrupted the install somehow, it is the same process to install it to your system, expect instead of python2 you will type python3 like so:

Install Python 3 Command

sudo apt-get install python3

Install any version of Python

As you can see installing python to linux, especially on Ubuntu is rather simple and fast. Of course you can also very easily specify the exact version that you would like to install as well, if you need it for a specific project you are working on. To do so, simply type out the following command:

Install any version of Python

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.5

Lets break down what these commands are doing and how to use them. The first command is adding a new repository for your system to pull from. Think of a repository as if it were a box that holds all your favorite software inside that you can easily download at any time you wish. In fact you are already using repositories with the installation method of the most recent versions of python, and those just happened to be included by default when you install Ubuntu for you.

Next we are updating the system and packages to make sure everything is properly configured and seen.

Finally we are saying hey, grab this package names python3.5 for me and download it and install it.

Anytime in the future that you need a different version of python, you can now specify the exact version number that you need and you are able to get the version directly in your terminal without having to build python from source or from a wheel file or anything else like that. It really is the easiest solution.

Run Python in the Terminal

Now how do you use the versions of Python that you have installed on the system? Well that is quite simple with the following command:

How to open and use python in the terminal

python2
python3
python3.5

So each command would open a different version of python to use. Just use one command at a time depending on what version you want to launch inside of your terminal. python2 will open the interactive environment in the terminal for the latest version of Python 2 that you have installed. python3 will open the latest version of python3 that you have installed. If you have installed any of the python versions specifically like 3.5 then typing python3.5 will open that version for you.

Of course this is oversimplifying symlinking and how the system knows exactly what you open up, but you need not worry about that right now and just understand the system is able to keep track of your installs if you have installed them like I have indicated above.

Remove Installed Packages

If you ever need to remove a python install, or any package for that matter, it is generally as easy as:

Uninstall package

sudo apt autoremove package-name(python3, python2, etc.)

# example below to remove python2

sudo apt autoremove python2

When you type in this command it will tell you what will be uninstalled and ask you if you are sure. Type ‘Y’ if you want to remove the package from the system or ‘n’ if you do not want to.

Conclusion

So that about wraps up this tutoral. There is an accomanying video tutorial if that is more your speed and is less than 3 minutes long. Hope this tutorial helped to teach you a little about installing packages on Ubuntu and specifically how to install Python and its various versions. 

As always thank you all for your support and helping make this website the success that it is. While your hear, why not check out some of our other tutorials or share them with frends or coworkers? Until next time. 

Installing Python on Windows 10/11

Installing Python on Windows 10/11

Introduction

 

Hello and welcome to another tutorial on Learn Code Today. Today we are going to learn how to install Python to your computer. There are several different ways to install Python to our systems, but we are going to look at what I call a “Hard Install”. This means that we will install Python directly to our PC and not to something like Docker which containerizes installs so they don’t conflict with one another on a system. Let’s begin!

Step 1

 

To start, we need to navigate over to the Python website. Head over to https://www.python.org/ and you will see a downloads button right on the front page.

Python.org Downloads

Click on the “Downloads” button on the navigation bar, and it will take you to yet another page that will allow you to choose which version you would like to download. 

Python Download Link

Now quick caveat. This will always download the 32-bit installer. If you are looking for the 64-bit installer, you will need to scroll down the page until you see the spot that says, “Looking for a specific installer”. This is where you will click the latest version and be taken to another page that will have the source files, exe’s, and web installers in both 32-bit and 64-bit. 

Python Specific Installer
Python 64 Bit installer

So you can see in the picture directly above, I have indicated with check marks, which python installer is 64-bit. While 32-bit is often enough for most people, if you have more than 4GB of RAM in your PC, you can benefit from running 64-bit Python on your system. If you have less than 4GB, most likely you are only running a 32-bit version of Windows, and installation will fail when trying to install the 64-bit version.

Ok enough technical speak, lets move onto downloading and installing Python! So once you have clicked the version you would like, a download prompt will appear asking you where you would like to put the installer. You can rename the installer if you like and put it anywhere easy to remember on your system.

Python Installer Save Location

Step 2

 

Ok, once you have the installer saved to your system. Its now time to run it. Quick side note, if you downloaded one of the optional installers as I highlighted above for the 64-bit version, make sure you understand how to run it. If it was the zip file, extract the contents first, and then double click the installer. If it was the EXE, well then all you need to do is double click it just like those that download it directly from the button on the second page without going for the 64-bit version.

Ok, now that you have opened the installer, you will be greeted by a first screen. It is important that you check the box at the bottom that says, “Add Python 3.X to Path” or something along those lines. This will save you major headaches down the line when trying to run python or scripts from the command line. Once you have checked that box, you can proceed with the normal installation by selecting “Install Now”. If you prefer to install Python to a different location, go ahead with the custom install and provide the location and proceed as normal from there.

 

Python Installer Screen 1

Once you begin the actual installation, it should go pretty quick, depending on your internet connection. There will be a UAC popup requesting permission to continue, so make sure to approve that to get the actual installation underway. 

Python Installing

Once the installer has completed, one final screen will appear asking you if you want to disable the path length limit. I would advise selecting yes for this option. Clicking on it will bring about yet another UAC popup. Just confirm and it will take you back to the complete screen with a “close” button in the lower right hand corner. You can now safely close the installer and Python is successfully installed on your system!

Python Installer Success

Step 3

 

Congratulations! You can now start using python on your system. To get started, simply navigate to the windows start menu, and in the recent programs section at the very top, you should see something like this:

Recently Added Programs

Click on the IDLE option and an interactive display will appear allowing you to start coding in real time with Python. You can also just open up the Command Prompt (CMD) and type in Python and hit “Enter” or “Return” and it will launch an interactive version there as well. 

Python IDLE
Python CMD

Conclusion

 

That is it for this tutorial. I hope you found it helpful. If you are on MAC or Linux, the process is fairly straight forward as well. With MAC, you install python just like any other MAC program and there is a section on their website with the download and instructions on how to do so. For Linux, just use your favorite package manager or compile from source and you are good to go! Most Linux distros come with Python pre-installed already as it is, but it is often and older version. I assume also, that if you are on Linux, you are already fairly familiar with installing applications and decent when it comes to being “tech savvy” and are more than capable of figuring it out.

Pin It on Pinterest