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.pyWhen 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 --versionIf Python is installed, you will see something like:
Python 3.12.1If 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 EnterStep 2: Navigate to File Location
Use the cd command:
cd Desktop
//Or:
cd C:\Users\YourName\DesktopStep 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:
Open IDLE
Click File → Open
Select hello.py
Click Run → Run Module (F5)
How to Run a Python Script on macOS
Open Terminal.
Navigate to File
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:
Install VS Code
Install Python extension
Open folder
Open .py file
Click Run button
Or use terminal inside VS Code:
python hello.pyRunning a Python Script in PyCharm
Step 1: Install PyCharm
Download and install PyCharm.
Step 2: Open or Create a Python Script
- Open PyCharm and create a new Python project.
- Inside the project folder, create a new file (
hello.py).
Step 3: Run the Script
- Right-click on
hello.py. - Click Run ‘hello’.
- 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
| Method | Best For | Difficulty | Recommended |
|---|---|---|---|
| Command Prompt | Beginners | Easy | ✅ Yes |
| VS Code | Developers | Easy | ✅ Yes |
| PyCharm | Large Projects | Medium | ✅ Yes |
| IDLE | Basic Learning | Easy | Yes |
| Double Click | Quick Run | Easy | ❌ Not Recommended |
How Python Executes a Script
When you run a Python script:
Python interpreter reads the file
Converts code to bytecode
Executes line by line
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:
Output:
Argument: TestRunning Python Script Automatically
You can make script executable (Linux/macOS).
Add at top:
#!/usr/bin/env python3//Then:
chmod +x hello.py
./hello.pyCommon 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
| Feature | Script Mode | Interactive Mode |
|---|---|---|
| File Saved | Yes | No |
| Best For | Projects | Testing |
| Reusable | Yes | No |
| Beginner Friendly | Yes | Yes |
//Interactive mode:
pythonBest IDEs to Run Python Script
| IDE | Free | Beginner Friendly | Advanced Features |
|---|---|---|---|
| VS Code | Yes | Yes | Yes |
| PyCharm | Community | Yes | Yes |
| IDLE | Yes | Yes | Limited |
| Jupyter | Yes | Yes | Data 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.pySecurity 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?