How to Run Python Code
The REPL, .py files, and the terminal
In this lesson
Once you’ve written some Python, how do you actually run it? There are two everyday ways: typing code into the quick interactive prompt, or saving it in a .py file and running it with the python command (also written python3 or py on some systems). You’ll learn when to use each and how the computer finds the file you want to run.
Explain it like I’m 5
The REPL is a calculator you talk to one line at a time. A .py file is a saved to-do list the computer runs from top to bottom all at once.
The REPL: instant experiments
Script files: real programs
For anything you want to keep or run again, put your code in a file ending in .py (for example hello.py). Then run the whole file at once from a terminal with python3 hello.py. The interpreter executes the file from the first line to the last.
# In the REPL you would type each line and see results immediately:
# >>> 2 + 2
# 4
# >>> name = "Ada"
# >>> name
# 'Ada'
print("This file ran top to bottom.")
This file ran top to bottom.
This editor behaves like a .py file: it runs every line from top to bottom. Add a second print() and run it.
print("Line one")
print("Line two")
Each print() on its own line produces its own line of output, in order.
print("Line one")
print("Line two")
print("Line three")
Line one
Line two
The working directory
When you run python3 hello.py, Python looks for hello.py in your terminal’s current working directory (the folder you’re ‘in’). If you get ‘No such file or directory’, you’re probably in the wrong folder. Use cd (change directory) to move into the folder that contains your file first.
Other ways you’ll run Python
The REPL and python file.py cover almost everything, but you’ll meet a few other ‘front doors’ to the same interpreter:
- Your editor’s Run button — runs the file you’re editing, showing output in a built-in panel.
python -m name— runs an installed module as if it were a program. For example,python -m http.serverstarts a tiny web server.- Jupyter notebooks — interactive documents that mix runnable code cells with notes, popular in data work.
They all execute the very same Python; they’re just different ways in.
python3 -m http.server 8000
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
Common mistake: Running a file from the wrong folder
Python searches the current working directory for the filename you gave it. If your terminal is in a different folder, it can’t find the file.
cd into the folder that contains your file first, then run it. Or pass the full path: python3 /full/path/to/hello.py.
cd my_project
python3 hello.py
How do you run a saved file named app.py?
Give the interpreter the filename: python3 app.py (from the folder containing it).
What is the REPL best for?
The REPL gives instant feedback for trying things; files are for code you want to keep and re-run.
Mini exercise (easy)
Write a two-line program: the first line prints a greeting, the second prints today’s task. Imagine saving it as day1.py and running it.
Do it here. Finish the code below and press Run to try your answer right in the browser — no setup needed.
# Line 1: print a greeting
# Line 2: print today’s task
print("Hello!")
print("Today: ...")
Two print() calls, one per line.
print("Good morning!")
print("Today’s task: finish lesson 3.")