How to Run a Python Script (2024)

In this tutorial, we will go through the steps on how you can run a Python script on your computer.

How to Run a Python Script (1)

Executing a Python script is very easy within the terminal or IDE (Integrated Development Environment). You can identify a Python script by the .py file extension. This tutorial will touch on how to run the script within the terminal or in an IDE like Visual Studio Code (VSC).

For this tutorial to work correctly, you will need to install Python to your chosen operating system. We have tutorials on installing Python to Windows, macOS, and Linux. These are all straightforward and will not take long to complete.

The steps below will take you through all you need to know to run a Python script. If you encounter any issues, your system may not have Python configured correctly.

Run a Python Script in the Terminal

Running a Python script in the terminal is very straightforward but requires you have Python already installed. Below are a few short steps that you will need to complete to run a script successfully.

1. Load the terminal application on your chosen operating system. For example, on Windows, this is the command prompt. On macOS and Linux, it is known as terminal.

To open the command prompt on Windows, press the Windows Key + R, type in cmd, and press Enter.

Load the terminal application on your Mac. You can find it by going tolaunchpadand searchingterminal. Click onTerminal.

Linux will vary depending on the operating system. For example, you can open the terminal on Ubuntu by pressing CTRL + ALT + T.

2. In the terminal, navigate to the directory where your Python script is located by using the cd command.

For example, the command below will change my directory to the dev directory.

cd /home/dev/dev

3. Inside this folder, I have several Python scripts.

To run a Python script, I will need to write python3, followed by the script’s name. It is important to note that the python command may differ. For example, it might be just python or python3.1.

python3 example.py

4. If you want to run a Python script without preceding the script with python, you will need to specify a shebang line pointing to the Python environment. The shebang line needs to be at the top of the file.

Below is an example of a shebang line pointing to the location of Python 3.

#!/usr/bin/env python3

After adding the shebang line, if you try and run the script without the python command, the script should still run correctly. However, you may need to use chmod to make the file executable.

./example.py

Run a Python Script in an IDE

Most modern integrated development environments (IDE) will have an inbuilt method of testing code, such as Python scripts. However, the way you run and test code will vary from IDE to IDE, so you may need to look up instructions on how you can do it.

In this example, we will use Visual Studio Code, an IDE that is available on almost every operating system, including Ubuntu.

There are two ways you can run a Python script within VS code. The first method involves using the built-in run and debug mode. The second method will use the built-in terminal.

1. Load VS code on your computer and open the Python file you wish to test.

In the sidebar, click on the icon that has the play icon and a bug. Next, click on “Run and Debug”.

VS Code will now open the inbuilt terminal and run the file, as seen in the image below.

How to Run a Python Script (6)

2. To use the integrated terminal, right-click on your file in the explorer tab on the right-hand side. Then, click on the option “Open in Integrated Terminal“.

How to Run a Python Script (7)

This option will open a terminal at the bottom of the editor. VS code will set the directory to the location of your script. Here you can run your Python script from within the IDE.

For example, in the screenshot below, we write python dataypes.py to run our file.

How to Run a Python Script (8)

Conclusion

I hope by now you have a good understanding of how you can run a Python script on your operating system of choice. Python is an excellent choice for running scripts that handle various tasks, as it is a versatile programming language.

If you are new to Python, we recommend checking out some tutorials to learn more about Python and its characteristics. Learning the Python syntax or the variables you can use is a great place to start.

Please let us know if you notice a mistake or an important topic is missing from this guide.

Recommended

Global Variables in Python

Check if a File Exists Using Python

Installing Python on Linux

Installing Python on macOS

How to use the Python Ternary Operator

Python Logical Operators

How to Run a Python Script (2024)
Top Articles
Academic Dishonesty - Community Standards and Conduct
What Does a Sound Business Strategy Look Like? 
Netronline Taxes
Victor Spizzirri Linkedin
Warren Ohio Craigslist
Trevor Goodwin Obituary St Cloud
12 Rue Gotlib 21St Arrondissem*nt
Practical Magic 123Movies
Bustle Daily Horoscope
Rainfall Map Oklahoma
What’s the Difference Between Cash Flow and Profit?
Everything You Need to Know About Holly by Stephen King
Zürich Stadion Letzigrund detailed interactive seating plan with seat & row numbers | Sitzplan Saalplan with Sitzplatz & Reihen Nummerierung
Leeks — A Dirty Little Secret (Ingredient)
Craiglist Kpr
使用 RHEL 8 时的注意事项 | Red Hat Product Documentation
Uta Kinesiology Advising
The Blind Showtimes Near Amc Merchants Crossing 16
Stoney's Pizza & Gaming Parlor Danville Menu
67-72 Chevy Truck Parts Craigslist
Www Va Lottery Com Result
2000 Ford F-150 for sale - Scottsdale, AZ - craigslist
Margaret Shelton Jeopardy Age
Enduring Word John 15
10 Best Quotes From Venom (2018)
WOODSTOCK CELEBRATES 50 YEARS WITH COMPREHENSIVE 38-CD DELUXE BOXED SET | Rhino
County Cricket Championship, day one - scores, radio commentary & live text
R/Orangetheory
Donald Trump Assassination Gold Coin JD Vance USA Flag President FIGHT CIA FBI • $11.73
Emiri's Adventures
Wbli Playlist
Tenant Vs. Occupant: Is There Really A Difference Between Them?
Oreillys Federal And Evans
AsROck Q1900B ITX und Ramverträglichkeit
Build-A-Team: Putting together the best Cathedral basketball team
Toonily The Carry
World History Kazwire
Frcp 47
Metro Pcs Forest City Iowa
Differential Diagnosis
Foxxequeen
Gli italiani buttano sempre più cibo, quasi 7 etti a settimana (a testa)
Frontier Internet Outage Davenport Fl
What is a lifetime maximum benefit? | healthinsurance.org
New Zero Turn Mowers For Sale Near Me
De boeken van Val McDermid op volgorde
Diesel Technician/Mechanic III - Entry Level - transportation - job employment - craigslist
Epower Raley's
What Responsibilities Are Listed In Duties 2 3 And 4
Ranking 134 college football teams after Week 1, from Georgia to Temple
Latest Posts
Article information

Author: Margart Wisoky

Last Updated:

Views: 5465

Rating: 4.8 / 5 (78 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Margart Wisoky

Birthday: 1993-05-13

Address: 2113 Abernathy Knoll, New Tamerafurt, CT 66893-2169

Phone: +25815234346805

Job: Central Developer

Hobby: Machining, Pottery, Rafting, Cosplaying, Jogging, Taekwondo, Scouting

Introduction: My name is Margart Wisoky, I am a gorgeous, shiny, successful, beautiful, adventurous, excited, pleasant person who loves writing and wants to share my knowledge and understanding with you.