How to Run a Python Script – A Beginner’s Guide

How to Run a Python Script – A Beginner's Guide

If you’re learning Python, one of the first things you need to understand is how to run a Python script. Writing code is only half the job — executing it correctly is what brings your program to life.

This beginner-friendly guide explains how to run a Python script on Windows, macOS, and Linux. You’ll also learn how to execute Python files using Command Prompt, Terminal, VS Code, PyCharm, and IDLE.

By the end of this guide, you’ll confidently know how to run a Python script in any environment.

What Is a Python Script?

A Python script is a file containing Python code. These files usually have the extension:

filename.py
//For example:

hello.py
calculator.py
app.py

When you run a Python script, the Python interpreter reads the file and executes the instructions line by line

Step 1: Install Python

Before learning how to run a Python script, you must install Python.

Check if Python Is Already Installed

Open Command Prompt (Windows) or Terminal (Mac/Linux) and type:

 
python --version
or

python3 --version

If Python is installed, you will see something like:

Python 3.12.1

If not, download it from:

https://www.python.org/downloads/

During installation on Windows, make sure to check:

✔ Add Python to PATH

This ensures you can run Python from the command line.

Step 2: Create a Python Script

Create a file named:

 
hello.py

Add this code:

 
print("Hello, World!")

Save the file.

Now you are ready to run a Python script.

How to Run a Python Script on Windows

There are multiple ways to run a Python script on Windows.

Method 1: Run Python Script Using Command Prompt (CMD)

Step 1: Open Command Prompt

Win + R → type cmd → Press Enter

Step 2: Navigate to File Location

Use the cd command:

cd Desktop
//Or:
cd C:\Users\YourName\Desktop

Step 3: Run the Python Script

Use the following command to run your script:

python hello.py

If you have multiple versions of Python installed, you might need to use:

python3 hello.py

Output:

Hello, World!

Method 2: Double Click the .py File

You can double-click the file.

⚠ However:

  • The window may close immediately.

  • Not recommended for beginners.

Better option:

input("Press Enter to exit")

Method 3: Run Python Script in IDLE

DLE comes installed with Python.

Steps:

  1. Open IDLE

  2. Click File → Open

  3. Select hello.py

  4. Click Run → Run Module (F5)

How to Run a Python Script on macOS

Open Terminal.

Navigate to File

 cd Desktop

Run Script

 python3 hello.py

//On macOS, always use python3.

How to Run a Python Script on Linux

Open Terminal.

Run:

python3 hello.py

Linux systems usually use python3 by default.

Running Python Script Using VS Code

VS Code is very popular among developers.

Steps:

  1. Install VS Code

  2. Install Python extension

  3. Open folder

  4. Open .py file

  5. Click Run button

Or use terminal inside VS Code:

python hello.py

Running a Python Script in PyCharm

Step 1: Install PyCharm

Download and install PyCharm.

Step 2: Open or Create a Python Script

  1. Open PyCharm and create a new Python project.
  2. Inside the project folder, create a new file (hello.py).

Step 3: Run the Script

  1. Right-click on hello.py.
  2. Click Run ‘hello’.
  3. The output will appear in the Run window.

Best Practice: Use PyCharm’s built-in terminal (Alt + F12) to run scripts manually.

Different Ways to Run a Python Script

MethodBest ForDifficultyRecommended
Command PromptBeginnersEasy✅ Yes
VS CodeDevelopersEasy✅ Yes
PyCharmLarge ProjectsMedium✅ Yes
IDLEBasic LearningEasyYes
Double ClickQuick RunEasy❌ Not Recommended

How Python Executes a Script

When you run a Python script:

  1. Python interpreter reads the file

  2. Converts code to bytecode

  3. Executes line by line

  4. Displays output

Understanding this helps when debugging errors.

Running Python Script with Arguments

You can pass arguments from command line.

Example:

 

import sys

print("Script name:", sys.argv[0])
print("Argument:", sys.argv[1])

Run:

 python hello.py Test

Output:

 Script name: hello.py
Argument: Test

Running Python Script Automatically

You can make script executable (Linux/macOS).

Add at top:

#!/usr/bin/env python3
//Then:

chmod +x hello.py
./hello.py

Common Errors When Running Python Script

1. Python Not Recognized

Error:

'python' is not recognized

Solution:
Reinstall Python and check “Add to PATH”.

2. File Not Found

Error:

 
No such file or directory

Solution:
Make sure you are in correct folder using cd.

3. Indentation Error

Python uses indentation.

Wrong:

 
if True:
print("Hello")

Correct:

 
if True:
print("Hello")

Running Python Script vs Interactive Mode

FeatureScript ModeInteractive Mode
File SavedYesNo
Best ForProjectsTesting
ReusableYesNo
Beginner FriendlyYesYes
//Interactive mode:

python

Best IDEs to Run Python Script

IDEFreeBeginner FriendlyAdvanced Features
VS CodeYesYesYes
PyCharmCommunityYesYes
IDLEYesYesLimited
JupyterYesYesData Science

Real-World Example: Running a Calculator Script

//Create:

num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

print("Sum:", num1 + num2)
//Run:

python calculator.py

Security Tip When Running Python Scripts

Never run unknown Python scripts downloaded from the internet.

They can:

  • Delete files

  • Access personal data

  • Install malware

Always review code before running.

FAQs – How to Run a Python Script

1. How do I run a Python script from command line?

Use:

python filename.py

Or:

 
python3 filename.py

2. Why is Python not recognized in CMD?

Because Python is not added to PATH. Reinstall Python and check “Add to PATH”.

3. Can I run Python without installing?

Yes, use online compilers like:

  • Replit

  • Google Colab

4. What is the difference between python and python3?

Some systems have Python 2 and Python 3 installed separately.

5. How do I stop a running Python script?

  • Related Posts

    How to Check Python Version – A Quick Guide

    How to Check Python Version – A Quick Guide If you are learning Python programming, installing libraries like NumPy, Pandas, or Django, or working on a machine learning project, the…

    How to Make a Table in HTML: A Step-by-Step Guide

    How to Make a Table in HTML: A Step-by-Step Guide Tables in HTML are a powerful way to display data in rows and columns. This guide will walk you through…

    Read More

    How to Create Database in MySQL

    • By admin
    • November 27, 2021
    • 64 views
    How to Create Database in MySQL

    How to create table in MySQL

    • By admin
    • November 6, 2021
    • 45 views
    How to create table in MySQL

    MySQL commands with examples

    • By admin
    • September 11, 2021
    • 136 views
    MySQL commands with examples

    MySQL use database

    • By admin
    • May 28, 2021
    • 44 views
    MySQL use database

    System Software

    • By admin
    • May 20, 2021
    • 65 views

    Introduction to software

    • By admin
    • May 13, 2021
    • 55 views
    Introduction to software