How to Run a Python Script - GeeksforGeeks (2024)

Last Updated : 21 Dec, 2023

Summarize

Comments

Improve

Python scripts are Python code files saved with a .py extension. You can run these files on any device if it has Python installed on it. They are very versatile programs and can perform a variety of tasks like data analysis, web development, etc.

You might get these Python scripts if you are a beginner in Python so in this discussion, we will explore various techniques for executing a Python script.

What is Python Script?

Python is a well-known high-level programming language. The Python script is a file containing Python-written code. The file containing Python script has the extension ‘.py’ or can also have the extension ‘.pyw’ if it is being run on a Windows 10 machine.

To run a Python script, we need a Python interpreter installed on the device. In this article, we will learn how to run a Python script.

Methods to Run a Script in Python

There are various methods to Run a Python script, we will go through some generally used methods for running a Python script:

  • Interactive Mode
  • Command Line
  • Text Editor (VS Code)
  • IDE (PyCharm)

How to Run a Python Script?

Let’s go through the basic steps and understand how a script works.

Here is a simple code to print ‘Hello World!’.

To Execute this program, first we have to save it with the ‘.py’ extension. Then we can execute this file with the help of the terminal.

Here, the print() function is to print out any text written within the parenthesis. We can write the text that we want to be printed using either a single quote as shown in the above script or a double quote.

If you are coming from any other language then you will also notice that there is no semicolon at the end of the statement as, with Python, you do not need to specify a semicolon at the end of the line. And also we don’t need to include or import any files to run a simple Python script.

There are various ways to run a script in Python but before going toward the different ways to run a Python script, we first have to check whether a Python interpreter is installed on the system or not. So in Windows, open ‘cmd’ (Command Prompt) and type the following command.

python -V

This command will give the version number of the Python interpreter installed or will display an error if otherwise.

How to Run a Python Script - GeeksforGeeks (1)

1. Run Python Script Interactively

In Python Interactive Mode, you can run your script line by line in a sequence. To enter an interactive mode, you will have to open Command Prompt on your Windows machine, type ‘python’ and pressEnter.

How to Run a Python Script - GeeksforGeeks (2)

Example1: Using Print Function

Run the following line in the interactive mode:

print('Hello World !')

Output:

How to Run a Python Script - GeeksforGeeks (3)

Example 2: Using Interactive Execution

Run the following lines one by one in the interactive mode.

name = "Aakash"
print("My name is " + name)

Output:

How to Run a Python Script - GeeksforGeeks (4)

Example 3: Interactive Mode Comparison

Run the following lines one by one in the interactive mode.

a = 1
b = 3
if a > b:
print("a is Greater")
else:
print("b is Greater")

Output:

How to Run a Python Script - GeeksforGeeks (5)

Note:To exit from this mode, press ‘Ctrl+Z’ and then press ‘Enter’ or type ‘exit()’ and then pressEnter.

2. Run Python Script by the Command Line

Running Python scripts on Windows via the command line provides a direct and efficient way to execute code. It allows for easy navigation to the script’s directory and initiation, facilitating quick testing and automation.

Example 1: Using Script Filename

To run Python in the terminal, store it in a ‘.py’ file in the command line, we have to write the ‘python’ keyword before the file name in the command prompt. In this way we can run Python programs in cmd.

python hello.py

You can write your own file name in place of ‘hello.py’.

Output:

How to Run a Python Script - GeeksforGeeks (6)

Example 2:Redirecting output

To run a Python script in Terminal from the command line, navigate to the script’s directory and use the python script_name.py command. Redirecting output involves using the > symbol followed by a file name to capture the script’s output in a file. For example, python script_name.py > output.txt redirects the standard output to a file named “output.txt.”

Output :

How to Run a Python Script - GeeksforGeeks (7)

Output

3. Run a Script in Python using a Text Editor

To run Python script on a text editor likeVS Code (Visual Studio Code)then you will have to do the following:

  • Go to the extension section or press ‘Ctrl+Shift+X’ on Windows, then search and install the extension named ‘Python’ and ‘Code Runner’. Restart yourvs codeafter that.
  • Now, create a new file with the name ‘hello.py’ and write the below code in it:
print('Hello World!')
  • Then, right-click anywhere in the text area and select the option that says ‘Run Code’ or press ‘Ctrl+Alt+N’ to run the code.

Output:

How to Run a Python Script - GeeksforGeeks (8)

4. Run Python Scripts using an IDE

To run Python script on anIDE (Integrated Development Environment) like PyCharm, you will have to do the following:

  • Create a new project.
  • Give a name to that project as ‘GfG’ and click on Create.
  • Select the root directory with the project name we specified in the last step.Right-clickon it, go toNew, anto,and click on the ‘Python file’ option. Then give the name of the file as ‘hello’ (you can specify any name as per your project requirement). This will create a ‘hello.py’ file in the project root directory.

Note:You don’t have to specify the extension as it will take it automatically.

How to Run a Python Script - GeeksforGeeks (9)

  • Now write the below Python script to print the message:
print('Hello World !')
  • To run this Python script, Right click and select the ‘Run File in Python Console’ option. This will open a console box at the bottom and show the output there. We can also run using the Green Play Button at the top right corner of the IDE.

How to Run a Python Script - GeeksforGeeks (10)

Output:

How to Run a Python Script - GeeksforGeeks (11)

We have covered 4 different methods to run Python scripts on your device. You can use any of the above methods depending on your device. Running Python scripts is a very basic and easy task. It helps you in sharing your work and accessing other source work for learning.

Similar Reads:

  • Run Python script from anywhere in linux
  • Run function from the command line In Python
  • How to Run a Python Script using Docker?
  • Schedule a Python Script to Run Daily


aakashpawar1999

How to Run a Python Script - GeeksforGeeks (13)

Improve

Next Article

How to run bash script in Python?

Please Login to comment...

How to Run a Python Script - GeeksforGeeks (2024)
Top Articles
What’s the difference between minimalism and frugality?
Frequently Asked Questions | FIRST4MAGNETS
South Park Season 26 Kisscartoon
Craigslist Mexico Cancun
Kentucky Downs Entries Today
Pbr Wisconsin Baseball
Goldsboro Daily News Obituaries
Tokioof
Wisconsin Women's Volleyball Team Leaked Pictures
TS-Optics ToupTek Color Astro Camera 2600CP Sony IMX571 Sensor D=28.3 mm-TS2600CP
National Weather Service Denver Co Forecast
Louisiana Sportsman Classifieds Guns
Cinebarre Drink Menu
Katherine Croan Ewald
2016 Hyundai Sonata Refrigerant Capacity
Nurse Logic 2.0 Testing And Remediation Advanced Test
Graphic Look Inside Jeffrey Dahmer
Craigslist Northfield Vt
Routing Number For Radiant Credit Union
Seeking Arrangements Boston
Sunset Time November 5 2022
Www.paystubportal.com/7-11 Login
Greyson Alexander Thorn
Mini Handy 2024: Die besten Mini Smartphones | Purdroid.de
Avatar: The Way Of Water Showtimes Near Maya Pittsburg Cinemas
Regina Perrow
Idle Skilling Ascension
Darktide Terrifying Barrage
Sinai Sdn 2023
Brenda Song Wikifeet
South Florida residents must earn more than $100,000 to avoid being 'rent burdened'
Elanco Rebates.com 2022
Nextdoor Myvidster
Craigslist Dallastx
Here’s how you can get a foot detox at home!
Newcardapply Com 21961
Edward Walk In Clinic Plainfield Il
Merge Dragons Totem Grid
Puffco Peak 3 Red Flashes
That1Iggirl Mega
Raising Canes Franchise Cost
Gifford Christmas Craft Show 2022
Umiami Sorority Rankings
ESA Science & Technology - The remarkable Red Rectangle: A stairway to heaven? [heic0408]
Chase Bank Zip Code
Best Suv In 2010
Gt500 Forums
Motorcycles for Sale on Craigslist: The Ultimate Guide - First Republic Craigslist
Who uses the Fandom Wiki anymore?
4015 Ballinger Rd Martinsville In 46151
Cheryl Mchenry Retirement
Latest Posts
Article information

Author: Jamar Nader

Last Updated:

Views: 5418

Rating: 4.4 / 5 (75 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Jamar Nader

Birthday: 1995-02-28

Address: Apt. 536 6162 Reichel Greens, Port Zackaryside, CT 22682-9804

Phone: +9958384818317

Job: IT Representative

Hobby: Scrapbooking, Hiking, Hunting, Kite flying, Blacksmithing, Video gaming, Foraging

Introduction: My name is Jamar Nader, I am a fine, shiny, colorful, bright, nice, perfect, curious person who loves writing and wants to share my knowledge and understanding with you.