杰瑞科技汇

Python interpreter是什么?如何运行Python代码?

Of course! Let's break down the Python interpreter from the ground up. It's one of the most fundamental concepts in learning Python.

Python interpreter是什么?如何运行Python代码?-图1
(图片来源网络,侵删)

What is a Python Interpreter?

At its core, the Python interpreter is a computer program that reads and executes Python code.

Think of it like a translator for a conversation. You speak Python, and the interpreter translates your instructions into a language the computer's hardware (the CPU) can understand and perform.

It does this in a two-step process for each line of code:

  1. Parsing: It reads your code, checks the syntax (the grammar), and converts it into an internal structure called an Abstract Syntax Tree (AST).
  2. Execution: It then executes the instructions in the AST.

This "read and execute" behavior is why Python is often called an interpreted language, as opposed to a compiled language like C++ or Java where you first compile the entire source code into an executable file before running it.

Python interpreter是什么?如何运行Python代码?-图2
(图片来源网络,侵删)

How Does It Work? (The "Magic" Explained)

The process is more detailed than just two steps. Here's a simplified view of the journey your code takes:

  1. Source Code (.py file): You write your instructions in a text file, for example, hello.py.

  2. Lexical Analysis (Tokenizer): The interpreter breaks the source code into a stream of "tokens." These are the smallest meaningful units of the language.

    • In print("Hello, World!"), the tokens would be: print, , "Hello, World!", , and .
  3. Parsing (Syntax Analysis): The tokens are arranged into a tree-like structure called an Abstract Syntax Tree (AST). This represents the grammatical structure of your code.

    Python interpreter是什么?如何运行Python代码?-图3
    (图片来源网络,侵删)
    • The AST for our example would show that there's a "call" to a function named print, which has one "string literal" argument.
  4. Compilation (to Bytecode): This is a key step. The Python interpreter does compile your code, but not to machine code. It compiles the AST into bytecode.

    • Bytecode is a low-level, platform-independent set of instructions that is specific to Python's Virtual Machine (PVM).
    • This bytecode is saved in a file with a .pyc extension (Python Compiled). This is why running a Python script the second time is often faster—the .pyc file can be reused.
  5. Execution (by the PVM): The Python Virtual Machine (PVM) is the final, real "engine" of Python. It's a piece of software that takes your bytecode and executes it, one instruction at a time.

    The PVM is what actually interacts with your computer's operating system to perform actions, like printing text to the screen or reading a file.


How to Use the Python Interpreter

There are two primary ways to interact with the Python interpreter.

Interactive Mode (The REPL)

REPL stands for Read-Evaluate-Print-Loop. It's an interactive shell where you can type Python code one line at a time, and the interpreter immediately executes it and shows the result.

This is fantastic for:

  • Testing small snippets of code.
  • Experimenting with Python features.
  • Debugging.

How to start it: Open your terminal or command prompt and type python or python3 and press Enter.

$ python3
Python 3.10.4 (main, Jun 29 2025, 12:14:53) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

The >>> is the prompt, telling you the interpreter is ready for input.

Example of using the REPL:

>>> 2 + 2
4
>>> print("Hello from the REPL!")
Hello from the REPL!
>>> my_variable = 10
>>> my_variable * 5
50
>>> exit()
$

To exit the REPL, type exit() or press Ctrl+D.

Script Mode (Running a File)

This is how you run complete Python programs. You write your code in a file with a .py extension and tell the interpreter to execute that entire file.

Step 1: Create a file. Create a file named my_script.py and add the following code:

# my_script.py
print("Hello from a script!")
name = "Alice"
print(f"Welcome, {name}!")

Step 2: Run the file from your terminal. Use the python or python3 command followed by the filename.

$ python3 my_script.py

Output:

Hello from a script!
Welcome, Alice!

Common Python Interpreters

While the standard interpreter (CPython) is the most common, other interpreters exist, each with its own strengths:

  1. CPython (The Standard):

    • This is the default interpreter you get when you download Python from python.org.
    • Written in C.
    • When people say "Python interpreter," they usually mean CPython.
  2. Jython:

    • Written in Java.
    • Its key feature is that it allows Python code to run on the Java Virtual Machine (JVM) and seamlessly interact with Java libraries.
  3. IronPython:

    • Written for the .NET framework.
    • Allows Python code to run on the Common Language Runtime (CLR) and use .NET libraries.
  4. PyPy:

    • This is a very special one. It's an implementation of Python written in a restricted version of Python called RPython.
    • Its main advantage is a Just-In-Time (JIT) compiler, which can significantly speed up your code (often 4-5x faster than CPython for long-running applications).

Key Takeaways

Concept Description Analogy
Python Interpreter A program that reads, compiles to bytecode, and executes Python code. A chef who reads a recipe (your code) and cooks the dish.
REPL An interactive "Read-Eval-Print Loop" for running code line-by-line. A conversation with the chef. You give one instruction, they do it and tell you the result.
Bytecode A low-level, intermediate language that Python code is compiled into. The chef's internal notes and shortcuts. Not the final dish, but the instructions to make it.
PVM (Python VM) The virtual machine that executes the bytecode. The kitchen and all the tools the chef uses to follow the notes and cook the dish.
CPython The standard, official interpreter for Python. The head chef at the most famous restaurant.
分享:
扫描分享到社交APP
上一篇
下一篇