01 / 21
>_

Running Python Scripts

Learn how to write, save, and execute Python programs from the command line using the python file.py command.

Python

What Is a Python Script?

A Python script is a plain text file containing Python code, saved with the .py extension. Instead of typing commands one by one into an interactive prompt, you write all your instructions in a file and run them together.

The Big Picture: From Code to Output

1. Write hello.py 2. Run python hello.py 3. Output Hello, World! Text Editor Terminal / CMD Screen

The workflow is simple: write your code in a .py file, run it with the python command in a terminal, and see the results on screen.

Why Run Scripts Instead of Typing Live?

★ Reusable
Run the same code as many times as you want without retyping anything.
★ Editable
Fix bugs or add features by editing the file, then run again instantly.
★ Shareable
Send a .py file to anyone with Python installed and they can run your program.

The Core Command

python file.py Interpreter Your Script

Anatomy of python file.py

1
python (or python3): the command that invokes the Python interpreter installed on your machine.
2
file.py: the filename (with path if needed) of the script. It must end in .py by convention.
3
Execution order: Python reads the file top to bottom, running each line in sequence.

Interactive vs Script Mode

Interactive (REPL) Script Mode
How to start Type python alone Type python file.py
Code saved? No: lost when you quit Yes: stored in .py file
Best for Quick tests, math Real programs, projects
Runs how? One line at a time Whole file, top to bottom

How to Run Your First Script

1
Open a text editor (VS Code, Notepad, IDLE, nano, etc.).
2
Write your Python code, for example: print("Hello, World!")
3
Save the file with a .py extension, e.g., hello.py
4
Open a terminal (Command Prompt on Windows, Terminal on Mac/Linux).
5
Navigate to the folder containing the file using cd.
6
Type python hello.py and press Enter.

Example 1: Hello, World!

# File: hello.py
print("Hello, World!")

In the terminal:

$ python hello.py
Hello, World!

Example 2: Multiple Lines

# File: greet.py
name = "Alice"
age = 16
print("Hi, I'm", name)
print("I am", age, "years old")
$ python greet.py
Hi, I'm Alice
I am 16 years old

Example 3: Navigating to Your File

If your script is in a folder called projects on your Desktop, you must cd (change directory) there first.

# Windows
C:\Users\you> cd Desktop\projects
C:\Users\you\Desktop\projects> python greet.py

# Mac / Linux
$ cd ~/Desktop/projects
$ python3 greet.py

What Happens Under the Hood?

You type: python file.py Python reads the .py file (source code: plain text) Compiles to bytecode (internal step: automatic) Executes and produces output

python vs python3

python
Works on Windows and many Linux systems. May point to Python 2 on older machines.
python3
Explicitly calls Python 3. Required on macOS and some Linux distributions.
# Check your version:
$ python --version
Python 3.12.1

$ python3 --version
Python 3.12.1

Reading Error Messages

$ python hello.py
File "hello.py", line 1
  print("Hello)
             ^
SyntaxError: unterminated string literal
1
File name and line number: tells you exactly where the error is.
2
The caret (^): points to the spot Python got confused.
3
Error type and message: SyntaxError means Python cannot understand your code.

Common Mistakes

✕ Wrong directory
Running python hello.py from a folder that does not contain hello.py. Use cd to navigate first.
✕ Missing .py extension
Saving the file as hello instead of hello.py, or typing python hello without the extension.
✕ Using python inside Python
Typing python hello.py at the >>> prompt (inside Python) instead of in the system terminal.

Quick Reference Table

Task Command Example
Check Python version python --version Python 3.12.1
Run a script python file.py python hello.py
Navigate folders cd path cd Desktop/projects
List files ls / dir hello.py greet.py
Open interactive mode python (no file) >>>
Exit interactive mode exit() back to $

Question 1 of 5

Which command correctly runs a Python script called app.py from the terminal?

Question 2 of 5

You type python hello.py and get the error: "No such file or directory". What is the most likely cause?

Question 3 of 5

What is the difference between typing python alone and python file.py in the terminal?

Question 4 of 5

A student saves their code in a file called mycode.py but types python mycode (without the .py extension) in the terminal. What will most likely happen?

Question 5 of 5

You see the >>> prompt and type python hello.py. You get a SyntaxError. Why?