Running a Python script takes one command. Open a terminal in the folder that holds the file and type python followed by the file name. That’s how to run a Python script on Windows; on a Mac or Linux it’s usually python3:
python hello.py # Windows
python3 hello.py # macOS and Linux
My test machine runs Python 3.12.5 on Windows 11, and the Command Prompt screenshots show each command exactly as I typed it.
You’ll learn the Windows, Mac and Linux commands, the py launcher, passing arguments, running modules with -m, VS Code, and how to fix the usual errors.
How to run a Python script in the Windows Command Prompt
Save your code in a file that ends in .py, open Command Prompt or PowerShell, cd to that folder, and run it. Here’s a tiny script and two ways to start it:
import sys
import platform
print("Hello from a Python script!")
print("Python", platform.python_version(), "on", platform.system())
print("Script name:", sys.argv[0])
Output:
Hello from a Python script!
Python 3.12.5 on Windows
Script name: hello.py
Hello from a Python script!
Python 3.12.5 on Windows
Script name: hello.py

python hello.py and py hello.py both run the script and print the same thing.The py command is the Python launcher for Windows, installed with the python.org installer. It’s handy when you have several versions, since py -3.12 hello.py picks one.
If you have more than one Python installed, installing multiple Python versions explains how the launcher chooses.
Running a Python file in the terminal on Mac and Linux
macOS and most Linux distributions use python3, because python either doesn’t exist or points somewhere else. The steps are the same: open Terminal, change to the folder, run the file.
cd ~/Projects
python3 hello.py
You can also make a script run like a program. Add a shebang line at the very top, make the file executable once, then call it by name:
#!/usr/bin/env python3
print("Hello from a Python script!")
# in Terminal:
chmod +x hello.py
./hello.py
How do you pass arguments to a Python script?
Anything you type after the file name lands in sys.argv, a list of strings. Item 0 is the script name, and your arguments come after it:
import sys
if len(sys.argv) < 3:
print("Usage: python trip.py CITY MILES")
sys.exit(1)
city = sys.argv[1]
miles = float(sys.argv[2])
gas = miles / 30 * 3.45 # 30 mpg at $3.45 a gallon
print("Arguments:", sys.argv[1:])
print(f"Driving {miles:,.0f} miles to {city} costs about ${gas:,.2f} in gas")
Output:
Arguments: ['Denver', '1020']
Driving 1,020 miles to Denver costs about $117.30 in gas
Arguments: ['San Diego', '1350']
Driving 1,350 miles to San Diego costs about $155.25 in gas
Usage: python trip.py CITY MILES

Arguments always arrive as text, so convert numbers yourself with float() or int(). For real flags and help text, Python command-line arguments covers argparse.
Running a Python module with python -m
The -m switch runs a module by name instead of by file path. Python finds it on its import path, which is how tools like pip and venv are usually run:
# python -m runs a module by name; -c runs one line of code
print("Run the commands below instead of this file.")
Output:
October 2026
Mo Tu We Th Fr Sa Su
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
59.97

python -m calendar 2026 10 prints October 2026. -c runs a quick one-liner without a file.I use python -m pip install ... rather than plain pip, because it guarantees the package goes into the same Python that will run your script.
Why use if __name__ == “__main__” in a script?
When you run a file directly, Python sets __name__ to "__main__". When another file imports it, __name__ is the module name instead, so guarded code doesn’t run:
def sales_tax(amount, rate=0.0625):
return round(amount * rate, 2)
print("__name__ is", repr(__name__))
if __name__ == "__main__":
print(f"Run directly: tax on $80.00 is ${sales_tax(80.00):.2f}")
Output:
__name__ is '__main__'
Run directly: tax on $80.00 is $5.00
__name__ is 'taxes'
6.25

This lets one file work both as a script and as a module you import. There’s more in using the Python main function with arguments.
Running a Python script in VS Code, IDLE and PyCharm
Editors save you the typing. In VS Code, install Microsoft’s Python extension, open the file, and click the Run Python File button (the triangle) in the top-right corner. It runs the script in the built-in terminal.
In IDLE, open the file and press F5. In PyCharm, right-click inside the editor and choose Run. All three just run the same python yourfile.py command for you.
Not sure which editor to pick? Here’s my take on PyCharm vs VS Code for Python.
Fixing errors when a Python script won’t run
The most common error is a typo in the file name or running from the wrong folder. Python tells you it can’t open the file, and dir shows what’s really there:
print("Q3 report: $48,250.00 in sales")
Output:
python: can't open file 'C:\\pyguides\\runs\\runscript\\reprot_q3.py': [Errno 2] No such file or directory
report_q3.py
Q3 report: $48,250.00 in sales
Here are the other usual suspects and their fixes:
| What you see | What it means | Fix |
|---|---|---|
can't open file ... No such file or directory | Wrong name or wrong folder | cd to the folder, check with dir or ls |
'python' is not recognized | Python isn’t on PATH | Use py, or reinstall with “Add python.exe to PATH” ticked |
| Microsoft Store opens | The Store alias is answering | Turn off the python.exe app execution alias in Windows settings |
ModuleNotFoundError | Package installed in another Python | python -m pip install the package |
| Window flashes and closes | Double-clicked the .py file | Run it from a terminal so the output stays |
That ModuleNotFoundError case has its own guide: ModuleNotFoundError after pip install. And if each project needs its own packages, set up a virtual environment first.
Once your scripts run, these are good next steps:
- Environment variables in Python
- Convert a Python file to an .exe with PyInstaller
- pip install a specific version
- If else in Python
Frequently asked questions
How do I run a Python script from the command line?
Open a terminal in the script’s folder and type python script.py on Windows or python3 script.py on macOS and Linux.
What’s the difference between python and py on Windows?
python runs whichever Python is first on your PATH. py is the Windows launcher, which picks the newest installed version or the one you ask for, like py -3.12.
How do I run a Python file in the VS Code terminal?
Open the integrated terminal with Ctrl+` and type python yourfile.py, or click the Run Python File button once the Python extension is installed.
How do I run a Python script with arguments?
Type them after the file name, like python trip.py Denver 1020, and read them from sys.argv or with argparse.
Why does my Python script close immediately?
You probably double-clicked it, so the window closes when the script ends. Run it from Command Prompt instead, or add input("Press Enter to exit") at the end.
What does python -m do?
It runs a module by name rather than a file path, such as python -m pip or python -m venv .venv. All the switches are listed in the Python command line docs.

Bijay Kumar is a 13-time Microsoft MVP with more than 18 years in software development, and the founder of Python Guides and TSinfo Technologies. He started out building .NET and SharePoint solutions at HP, TCS and KPIT before moving into Python, machine learning and AI, and he also builds web apps with TypeScript and React. He writes the tutorials here himself, and every example is run before publishing so you see the real output. More about Bijay · Microsoft MVP profile · LinkedIn