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.
1. The Interactive Mode (REPL)
Section titled “1. The Interactive Mode (REPL)”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.
Launching the REPL
Section titled “Launching the REPL”Open your terminal and type python (or python3). The >>> prompt indicates Python is ready for your input.
$ python3Python 3.12.0 (main, Oct 2 2023, 00:00:00)>>>The Mechanics of the REPL
Section titled “The Mechanics of the REPL”- Read: The interpreter waits for you to type a complete statement.
- Eval: It parses and executes that statement in the current session’s memory.
- Print: If the statement returns a value (like
1 + 1), it automatically prints it to the screen. - Loop: It returns to the
>>>prompt, keeping all your previous variables in memory.
2. The Script Mode
Section titled “2. The Script Mode”For building applications, we write code in .py files and execute them in one go.
Execution Flow
Section titled “Execution Flow”When you run python script.py, the interpreter:
- Reads the entire file.
- Performs a syntax check.
- Executes every line from top to bottom.
- 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.
3. Advanced Execution Flags
Section titled “3. Advanced Execution Flags”Professional developers often use CLI flags to change how the interpreter behaves.
The Module Flag (-m)
Section titled “The Module Flag (-m)”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.
python -m pip install requestsThe Command Flag (-c)
Section titled “The Command Flag (-c)”Runs a string of Python code directly from the terminal without creating a file. Useful for quick system checks.
python -c "import os; print(os.getcwd())"The Interactive Flag (-i)
Section titled “The Interactive Flag (-i)”Runs a script and then drops you into the REPL with the script’s variables still in memory. This is incredible for debugging.
python -i my_script.py4. Under the Hood: The Bytecode Lifecycle
Section titled “4. Under the Hood: The Bytecode Lifecycle”When you run a Python script, two things happen that are usually hidden from you:
1. The Tokenizer & Parser
Section titled “1. The Tokenizer & Parser”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.
2. The Compiler
Section titled “2. The Compiler”The AST is converted into Bytecode. Bytecode instructions are much simpler than Python code (e.g., BINARY_ADD, LOAD_NAME).
3. The __pycache__ Folder
Section titled “3. The __pycache__ Folder”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.
5. Summary Table
Section titled “5. Summary Table”| Feature | Interactive Mode (REPL) | Script Mode |
|---|---|---|
| Primary Use | Testing, Debugging, Learning. | Application Development. |
| Persistence | State is kept until you exit. | State is wiped when script ends. |
| Output | Automatically prints expressions. | Only prints via print(). |
| File Required | No. | Yes (.py). |