Is Python Compiled or Interpreted?

Is Python compiled, interpreted, or both? The practical answer is: Python is usually described as an interpreted language, but CPython, the standard implementation most people use, first compiles source code to bytecode and then executes that bytecode with an interpreter.

Is Python compiled interpreted or both

This is why both answers can appear in discussions. Python does not normally require a separate compile-and-link step like C or C++. But internally, CPython still has a compiler stage that creates bytecode before execution.

Short Answer

  • For everyday use: Python is interpreted. You run python script.py and get output without manually producing an executable.
  • Inside CPython: Python source is compiled to bytecode, then that bytecode is executed by the bytecode interpreter.
  • For imports: CPython may cache bytecode in __pycache__ as .pyc files to speed up future imports.

The official Python tutorial says Python is interpreted because no separate compilation and linking step is necessary during development. The CPython internals documentation also describes the bytecode interpreter as the component that executes compiled Python code.

What Happens When You Run a Python File?

When you run a command like this:

python hello.py

CPython roughly follows this flow:

  1. Reads the source code from hello.py.
  2. Parses and compiles the source into Python bytecode.
  3. Creates code objects that contain bytecode instructions.
  4. Executes those bytecode instructions in the Python interpreter.

So Python is not interpreted in the sense that every source line is simply translated directly to machine code one by one. It has a real compilation step, but that step is usually automatic and hidden from the user.

What Is Python Bytecode?

Bytecode is a lower-level instruction format for the Python virtual machine. It is not native CPU machine code. That is the key difference between Python bytecode and a compiled C executable.

Python’s PEP 3147 explains that CPython compiles source code into bytecode and can cache that bytecode on the file system. That cached bytecode usually appears inside a __pycache__ directory.

Why Does Python Create .pyc Files?

When a module is imported, CPython may save compiled bytecode as a .pyc file. On later imports, Python can reuse the cache if it still matches the source file and Python version. This avoids repeating part of the compilation work.

You can also compile a Python file manually with the standard py_compile module:

python -m py_compile hello.py

That creates bytecode, but it still does not turn Python into a standalone native executable. The bytecode is meant to be run by a compatible Python interpreter.

Compiled vs Interpreted in Python

QuestionAnswer for CPython
Does Python check and compile source before running it?Yes, CPython compiles source to bytecode.
Does Python usually require a manual compile step?No, users normally run scripts directly.
Does Python bytecode equal native machine code?No, bytecode is executed by the Python interpreter.
Can Python cache compiled bytecode?Yes, imported modules may create __pycache__ and .pyc files.

Compile-Time vs Runtime Errors

Some Python errors happen before execution because the compiler cannot parse the program. For example, this code has invalid syntax:

if True
    print("missing colon")

Python raises a SyntaxError before the program runs. Other errors happen at runtime, after bytecode execution begins:

print(10 / 0)

This raises ZeroDivisionError while the code is executing. This distinction is one reason the simple labels “compiled” and “interpreted” can be misleading. If you want tooling help, see this guide to Python syntax checkers.

Conclusion

Python is best described as interpreted from the user’s point of view because you normally run scripts directly with the Python interpreter. At the CPython implementation level, it is both compiled and interpreted: source code is compiled to bytecode, and that bytecode is executed by the interpreter. The important part is that Python bytecode is not the same as native machine-code compilation.

Sources: Python tutorial, PEP 3147, and CPython bytecode interpreter internals.

Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Stevo Devo
Stevo Devo
4 years ago

Java works the same way. The javac command compiles to bytecode and then the bytecode is interpreted by the JVM.

Last edited 4 years ago by Stevo Devo