Skip to content

The Python Interpreter

The “Python” you installed is actually the CPython Interpreter. This program is the engine that drives your code. Understanding the different ways to interact with this engine is essential for debugging, testing, and deploying professional software.


The REPL is a “Read-Eval-Print Loop.” It is a live conversation with Python where you type code, and it immediately gives you feedback.

Open your terminal and type python (or python3). The >>> prompt indicates Python is ready for your input.

$ python3
Python 3.12.0 (main, Oct 2 2023, 00:00:00)
>>>
  1. Read: The interpreter waits for you to type a complete statement.
  2. Eval: It parses and executes that statement in the current session’s memory.
  3. Print: If the statement returns a value (like 1 + 1), it automatically prints it to the screen.
  4. Loop: It returns to the >>> prompt, keeping all your previous variables in memory.

For building applications, we write code in .py files and execute them in one go.

When you run python script.py, the interpreter:

  1. Reads the entire file.
  2. Performs a syntax check.
  3. Executes every line from top to bottom.
  4. Terminates. The memory is wiped once the script finishes.

Unlike the REPL, script mode does not automatically print values. You must use the print() function explicitly.


Professional developers often use CLI flags to change how the interpreter behaves.

This runs a library module as a script. It is the safest way to run tools like pip or venv because it ensures you are using the version of the tool tied to that specific Python interpreter.

Terminal window
python -m pip install requests

Runs a string of Python code directly from the terminal without creating a file. Useful for quick system checks.

Terminal window
python -c "import os; print(os.getcwd())"

Runs a script and then drops you into the REPL with the script’s variables still in memory. This is incredible for debugging.

Terminal window
python -i my_script.py

When you run a Python script, two things happen that are usually hidden from you:

Python first breaks your text into “tokens” (like keywords, variables, and operators) and builds an Abstract Syntax Tree (AST). This is where syntax errors are caught.

The AST is converted into Bytecode. Bytecode instructions are much simpler than Python code (e.g., BINARY_ADD, LOAD_NAME).

To avoid re-compiling every time, Python saves this bytecode in .pyc files inside a __pycache__ directory. If the source file hasn’t changed, Python skips the compilation step and runs the cached bytecode immediately, making the program start faster.


FeatureInteractive Mode (REPL)Script Mode
Primary UseTesting, Debugging, Learning.Application Development.
PersistenceState is kept until you exit.State is wiped when script ends.
OutputAutomatically prints expressions.Only prints via print().
File RequiredNo.Yes (.py).