How to Run Python Code

The REPL, .py files, and the terminal

Beginner 8 min

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

Type python3 (or python) with nothing after it and you enter the REPL — Read, Evaluate, Print, Loop. You type one line, press Enter, and immediately see the result. It’s perfect for trying an idea, checking what a function does, or doing quick maths. Type exit() to leave.

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.

Example
# 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.")
Output
This file ran top to bottom.
A comment showing REPL interaction, plus a line that runs as a script.

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")

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.server starts 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.

Example
python3 -m http.server 8000
Output
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
Running a standard-library module as a program. (Run locally; Ctrl+C to stop.)

Common mistake: Running a file from the wrong folder

Why it happens:

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.

How to fix it:

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?

What is the REPL best for?

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: ...")

What to learn next

You learned the difference between the REPL, ideal for trying one line at a time, and .py files for programs you save and re-run, plus how your working directory affects running a file. You practised by printing a greeting and the day’s task.

Now that you can run code, it’s worth getting fluent with the instruction you’ll use more than any other. Your First Python Program digs into print(): how it handles text versus numbers, printing several values at once, and why quotes and parentheses matter. When you’re ready, continue to the next lesson.