Python Programming · Foundations

Running Python Programs

Want it in plain words first? Jump to Eli explains — the same idea, no jargon.
On this page 9 sections
  1. In 30 seconds
  2. Why this matters
  3. The college version
  4. Eli explains
  5. Worked example
  6. Key takeaway
  7. Quick check
  8. Study tools
  9. Sources & references

In 30 seconds

Python can execute a small expression interactively or run the statements stored in a .py file. The interactive interpreter is useful for quick checks; a makes a repeatable program you can run again. When Python runs a file as the top-level program, that file's name is 'main'. Output shows normal progress, while a tells you where an unhandled error stopped execution.

Why this matters

Running code is the boundary between an idea that looks plausible and behavior you have actually observed. Interactive checks help you test a small expression before you build on it. Scripts preserve a sequence of steps so you, a classmate, or a grader can repeat the same run. Reading the output and the last line of a traceback also gives you a concrete starting point when a program does not finish.

The college version

Two places Python can execute code

Python's interpreter is a program that reads Python code and executes it. One way to use it is interactively: start the interpreter with no program file and, when standard input is a terminal, it presents a primary prompt. You enter one expression or statement at a time. For example, entering 2 + 3 produces 5. That short feedback loop is useful when you want to check what a small expression does. It is not a permanent record of a program unless you separately save the code.

The other common mode is a script: a text file containing Python code, conventionally named with .py. Giving that file's path to the interpreter asks Python to read and execute the file. From the directory that contains hello.py, the command python3 hello.py runs it. The important general idea is not one shell spelling: identify the your environment provides, then pass it the path to the script. An IDE's Run button normally performs that same broad operation for a selected file, but you should still know which file it is running and where its output appears.

A script is valuable because the exact statements can be run again. If hello.py contains print("hello from a file"), each successful run writes that line to . The lesson is not that every program should be launched from a terminal; it is that the file, interpreter, current location, and output panel are the pieces to check when you ask, “What did I run?”

Top-level execution and the main guard

Every module has a __name__ value. Python documents a special environment named __main__. When Python executes a file as the top-level program, it sets that file's __name__ to the string '__main__'. That makes this small conditional useful:

if __name__ == "__main__": announce()

The indented call runs when the file is launched as a program. If another program loads the file to use its definitions, the condition is false because the loaded module has its module name instead. The guard is therefore a narrow control over what happens automatically at top level. It is not a requirement for every tiny script, and it does not replace a full discussion of imports or module design. Its beginner-level value is simple: define reusable pieces above it, then put the “start this program” action inside it.

For instance, a file can define announce() to print started, then call it only under the . Direct execution prints started. Loading the same file under another module name makes the function available without that automatic print. This distinction prevents surprising side effects when code meant for reuse is loaded elsewhere.

Observe output, errors, and the exact run

Program output is evidence of one execution. It tells you what the program wrote, but it must be read with the command or run configuration that produced it. A fast, documented option for a one-line experiment is python3 -c 'print(2 + 3)', which prints 5; this is convenient for a quick check but does not create a script file. For a saved program, rerunning the same file repeats its statements in order while no unhandled error intervenes.

When an is unhandled, Python prints a traceback to show the path of calls that led to it. In a simple top-level script, the traceback identifies the file and line that failed, and its final line names the exception. A file containing print("before"), then 1 / 0, then print("after") writes before, reports ZeroDivisionError: division by zero, and never reaches after. The path on your machine will differ, so do not memorize a path from an example; find the path and line in your own traceback.

This is observation, not yet a debugging method. Before changing code, verify that you ran the intended file, inspect the normal output in order, and read the exception type and location. Those checks avoid a common beginner confusion: editing one copy of a file while an IDE or terminal is executing another one.

Eli, the EliExplains learning guide

Eli explains

The same idea, in plain words

Explain it like I’m 10

Think of Python as a careful reader. You can hand it one tiny note at a time and it will answer right away; that is the interactive interpreter. Or you can hand it a page called a script and say, “Read this page from top to bottom.” A saved page is easier to run again tomorrow because the instructions stay put.

Sometimes the same page might be used in two ways: as the program itself, or as a collection of useful pieces for another program. The main guard is a note saying, “Only press the start button if this page is the program we launched.” When something goes wrong, Python leaves a traceback: a trail showing where it was when it tripped.

Picture it like this

Interactive mode is like asking a calculator one question at a time. A script is like saving a recipe: you can follow the same steps again. The main guard is a sign by the stove saying “turn on the oven only when cooking this recipe,” not when someone merely borrows the ingredient list.

Where the picture stops working

Python is more than a calculator or recipe card. Interactive entries can define names that later entries use, scripts can receive arguments and use files, and the exact effects of loading code are broader than this simple main-guard picture. The analogy only explains the basic choice of quick interaction versus a repeatable file.

Worked example

Create start.py with a function and a main guard: def announce(): followed by an indented print("started"), then if __name__ == "__main__": followed by an indented announce(). Running python3 start.py produced started when verified. Python runs this file as the top-level program, so the condition is true and calls the function. Next, consider a failing file with three top-level statements: print before; evaluate 1 / 0; print after. Its verified output begins with before, then ends in ZeroDivisionError: division by zero; after is absent because the unhandled exception stopped the sequence before that statement.

Key takeaway

Use interactive mode for small checks and a .py script for a repeatable program. Know what file ran, observe its output, and use the traceback's location and final exception line when execution stops.

Quick check

3 questions here, of 5 in this lesson’s practice set. Answers stay hidden until you check.

Question 1 of 3foundational

What does Python do when you give it a path to a .py script file?

Choose an answer, then check it.
Question 2 of 3intermediate

A learner wants to check the result of one small expression without making a file. Which mode best fits?

Choose an answer, then check it.
Question 3 of 3intermediate

During direct execution of a file, when does code inside if name == 'main': run?

Choose an answer, then check it.
Practice all 5

Keep learning

Ready to build on this? Continue to the next lesson.

Practice this lesson
Study tools & related lessonsYou’ll learn to · Common mistakes · Easily confused · Key vocabulary · Related

You’ll learn to

  • Distinguish interactive interpreter use from running a saved Python script.
  • Run a script by giving its path to the Python interpreter.
  • Explain the basic purpose of an if name == 'main' guard.
  • Interpret ordinary output and the final line of a simple traceback.
  • Choose an appropriate execution mode for a quick check or a repeatable program.

Common mistakes

  • Assuming an interactive experiment automatically becomes a saved program.

    Interactive entries are useful for checks, but save code in a .py file when you need a repeatable script.

  • Believing a Run button makes the file path irrelevant.

    An IDE abstracts the command, but you still need to know which file and configuration it executes and where it shows output.

  • Treating every traceback line as the error name.

    Read the file and line information, then use the final line to identify the exception type and its message.

  • Expecting statements after an unhandled exception to run.

    Execution stops at that point unless the exception is handled; observe which output appeared before it.

Easily confused

Interactive interpreter vs. Saved script

The interpreter accepts short entries immediately; a script stores a sequence in a file for repeatable execution.

Normal output vs. Traceback output

Normal output is what the program intentionally writes; a traceback reports an unhandled exception and where it occurred.

Direct execution vs. Loading code under another module name

Direct top-level execution gives the file name == 'main'; loading it under a module name does not.

Key vocabulary

Python interpreter
The program that reads Python code and executes it.
interactive mode
A mode in which the interpreter accepts code one entry at a time and displays results or errors immediately.
script
A saved text file containing program statements for an interpreter to execute.
standard output
The normal output stream where a program commonly writes displayed results.
traceback
Python's report of the call stack and exception when an exception is not handled.
exception
A runtime event that interrupts normal program flow unless code handles it.
top-level code
Code executing in the environment where Python starts a program, module, command string, or interactive session.
main guard
An if statement that tests whether name equals 'main' before performing a top-level action.

Sources & references

  1. Using the Python Interpreter — Python Documentation
  2. Command line and environment — Python Documentation
  3. __main__ — Top-level code environment — Python Documentation
  4. Errors and Exceptions — Python Documentation

EliExplains lessons are original prose written from the open, credible references above. See Copyright & Licensing.

Researched 2026-08-19

Educational content only. It is not medical, legal or professional advice. Found an error? Tell us.