PythonIntroduction to PythonPython fundamentals

Is Python a compiled or interpreted language?

Question 7 of 13 · Updated

On this page

Python is commonly described as an interpreted language, but that description needs an important qualification: the standard Python implementation, CPython, first compiles source code into bytecode and then executes that bytecode using its virtual machine. Python therefore uses both compilation and interpretation in its normal execution process.

The most accurate short answer is: Python is generally considered an interpreted, high-level language; CPython compiles source code to bytecode before the interpreter executes it. It does not normally compile an entire Python program into a standalone native machine-code executable in the way a traditional ahead-of-time C or C++ build does.

1. Why is Python called an interpreted language?#

A compiled language is often taught as one where a compiler translates source code into machine code before the program runs. An interpreted language is often taught as one where a runtime program reads or executes the source or an intermediate representation while the application is running.

These are useful introductory descriptions, not strict categories that every language fits into perfectly. Real language implementations can combine compilation and interpretation, use just-in-time compilation, or choose different execution strategies.

Python is called interpreted because a developer can normally run a source file directly with the Python runtime:

BASH
python app.py

You do not normally need to run a separate command that produces a native executable before launching the program. The Python runtime handles the work needed to prepare and execute the code.

However, directly running a .py file does not mean CPython executes the original source text one character at a time. CPython compiles the source into an internal bytecode representation first.

2. How CPython executes a Python program#

CPython is the reference implementation and the implementation most people install from python.org. Its normal execution path can be understood as these stages:

  1. Source code: You write Python statements in a .py file.
  2. Parsing and compilation: CPython parses the source, checks its syntax, and compiles it into code objects containing bytecode and related metadata.
  3. Optional bytecode caching: When a module is imported, CPython may save its compiled bytecode in a .pyc file under a __pycache__ directory. This cache can avoid compiling an unchanged module again on a later import.
  4. Execution: The CPython virtual machine executes the bytecode instructions. The interpreter implementation itself is native machine code running on the operating system and processor.

A simplified view is:

Python source (.py)
        |
        v
   Parser/compiler
        |
        v
  Code object / bytecode
        |
        +------> Optional .pyc cache (for imported modules)
        |
        v
 CPython virtual machine
        |
        v
 Program behaviour and output

This is a conceptual model. Runtime details, optimisations, and the exact internal steps can vary between Python versions.

What is bytecode?#

Bytecode is a lower-level, platform-specific instruction format used internally by a Python implementation. It represents operations such as loading values, calling functions, performing arithmetic, and returning results.

Bytecode is not the same thing as the original Python source code, and it is not generally a standalone native executable. In CPython, the virtual machine processes bytecode instructions and performs the corresponding operations.

For example, consider this small program:

PYTHON
price = 100 quantity = 3 total = price * quantity print(total)

The source expresses the calculation in readable Python syntax. CPython compiles it into bytecode instructions representing operations such as loading values, multiplying them, storing the result, and calling print().

You can inspect the bytecode for a function with the standard-library dis module:

PYTHON
import dis def calculate_total(price, quantity): return price * quantity dis.dis(calculate_total)

The output is a disassembly: a human-readable view of the bytecode instructions for that function. Exact instruction names and output can change across Python versions. The dis module documents CPython bytecode as an implementation detail, not a stable format for applications to depend on.

3. What is a Python virtual machine?#

A virtual machine (VM) is a software execution environment. In CPython, the Python virtual machine is the part of the runtime that executes Python bytecode.

It is not the same as a hardware CPU, and it does not mean that Python runs inside a full virtual computer such as a virtual machine used to host an operating system. It is a software component within the Python runtime.

The interpreter implementation is itself compiled into native machine code for the target platform. While running, it dispatches bytecode operations and carries out their meaning using the runtime's data structures, object model, memory management, and other services.

This distinction helps explain why the statement “Python compiles to bytecode” does not mean “Python is compiled to native machine code before execution.” Bytecode is an intermediate representation executed by the Python runtime.

4. What are .pyc files and the __pycache__ directory?#

When CPython imports a Python module, it can store the compiled bytecode in a .pyc file. These files are normally placed in a directory named __pycache__ next to the source module.

For example, importing a module named helpers.py may create a cache file resembling:

project/
    helpers.py
    __pycache__/
        helpers.cpython-3xx.pyc

The exact filename includes an implementation and version tag; the example is illustrative.

The purpose of this cache is to reduce repeated compilation work. If the source and cache are considered compatible, Python can load the cached bytecode instead of compiling the source again. Cache validation uses metadata or, in supported modes, a hash of the source. If the cache is missing, invalid, or out of date, Python can compile the source again.

Important points:

  • .pyc files are an optimisation, not a requirement for running Python programs.
  • Python can import a module even when it cannot write a cache file, provided the source is accessible and the runtime can compile it.
  • A .pyc file is not normally a portable executable that can be distributed and run independently of a compatible Python runtime.
  • Bytecode formats can change between Python versions and implementations. Do not treat .pyc files as a stable interchange format.
  • The main script passed directly to python app.py is not necessarily saved as a .pyc file. The familiar __pycache__ cache is primarily associated with imported modules.

You may also prevent Python from writing bytecode cache files by setting the PYTHONDONTWRITEBYTECODE environment variable. This does not turn off the compilation needed to execute source code; it prevents writing the cache files.

5. Is Python compiled or interpreted? The precise distinction#

The answer depends partly on what is meant by “Python.” It is useful to separate the language from a particular implementation.

  • Python language: The language defines syntax and semantics. It does not require every implementation to use one specific bytecode format or execution engine.
  • CPython: Compiles source into bytecode and executes it in its virtual machine. This is why Python is commonly classified as interpreted even though compilation is part of execution.
  • Other implementations: They may use different internal representations or execution strategies. Their behaviour should not be assumed to match every CPython implementation detail.

The phrase “Python is interpreted” is therefore a practical classification, not a complete description of every Python runtime.

6. Do all Python implementations work the same way?#

No. Python is a language with multiple implementations. CPython is the most widely used, but it is not the only one.

CPython#

CPython is implemented primarily in C and is the default implementation for many Python installations. It compiles Python source to CPython bytecode and executes that bytecode in its virtual machine. It is the implementation described by the .pyc and dis details above.

PyPy#

PyPy is another Python implementation. It includes a just-in-time (JIT) compiler that can identify frequently executed code paths and compile them into machine code while the program runs. This can improve performance for some workloads, although the result depends on the application, libraries, and workload. A JIT does not guarantee that every PyPy program will be faster than the equivalent CPython program.

Other implementations#

Jython, IronPython, and MicroPython are examples of implementations designed for different runtime environments or resource constraints. Their internals and compatibility characteristics differ. A CPython-specific detail—such as a particular bytecode instruction or .pyc layout—should not be assumed to apply to every implementation.

The important lesson is that Python the language is not identical to CPython the implementation.

7. What is the difference between compilation, interpretation, and JIT compilation?#

ApproachWhat happensTypical description
Ahead-of-time (AOT) compilationCode is compiled before execution, often into native machine code or another target formatCommon in compiled-language toolchains
InterpretationA runtime executes source code or an intermediate representationCommon description of interpreted execution
Bytecode compilation and VM executionSource is compiled into bytecode, which a virtual machine executesCPython's usual model
Just-in-time (JIT) compilationCode is compiled into machine code while the program is running, often after identifying frequently used pathsUsed by PyPy and several other runtimes

These categories can overlap. A runtime can compile code into bytecode, interpret that bytecode, and also use runtime compilation or specialisation to optimise execution. That is why a one-word label can hide important implementation details.

8. Does bytecode compilation make Python as fast as C or C++?#

No. The fact that CPython compiles source into bytecode does not mean it has the same execution model or performance characteristics as a typical C or C++ program compiled ahead of time into native machine code.

In ordinary CPython execution, bytecode operations are handled by the interpreter runtime. Dynamic type checks, object operations, function calls, and other runtime work can add overhead compared with native code specialised at compile time.

Python can still perform very well in many real applications. A large part of the work may be performed by libraries whose computationally intensive components are implemented in optimised C, C++, Fortran, or other native code. Some workloads can also benefit from JIT-enabled implementations or specialised tools.

When performance matters, measure the actual workload before choosing an implementation or optimisation. Algorithm choice, data structures, I/O, database queries, network calls, and native libraries can matter more than the language label alone.

9. Practical example: compile Python source explicitly#

Python provides a built-in compile() function. It demonstrates that compilation is an explicit stage that can be accessed from Python code:

PYTHON
source = "result = 10 * 5" code_object = compile(source, "<example>", "exec") namespace = {} exec(code_object, namespace) print(namespace["result"])

Output:

50

Here, compile() converts the source string into a code object. exec() then executes that code object in the supplied namespace. This example is useful for understanding the stages, but exec() should not be used with untrusted input: executing attacker-controlled code can compromise the application.

10. Common misconceptions#

  • “Python never compiles code.” Incorrect. CPython compiles source code into bytecode.
  • “A .pyc file is native machine code.” Incorrect. It contains cached Python bytecode and associated data, not a normal standalone native executable.
  • “Every Python script must have a .pyc file.” Incorrect. Cache files are optional; Python can compile source when needed.
  • “All Python implementations use CPython bytecode.” Incorrect. Bytecode and execution engines are implementation-specific.
  • “Because Python compiles to bytecode, it must be as fast as C.” Incorrect. Bytecode execution and ahead-of-time native compilation are different execution models.
  • “Interpreted means the source is read one character at a time.” This is an oversimplification. CPython parses and compiles source before executing the resulting code objects.

Frequently asked questions#

Is Python an interpreted language or a compiled language?#

Python is generally classified as an interpreted language. In CPython, source code is first compiled into bytecode and then executed by the Python virtual machine. Both compilation and interpretation are involved.

Does Python compile code before running it?#

Yes. CPython compiles source into code objects containing bytecode before execution. For imported modules, it may also cache that bytecode in .pyc files.

Does Python convert .py files into .exe files?#

Not as part of its normal execution process. Running python app.py uses a Python runtime to execute the program. Separate packaging tools can bundle a Python application and its dependencies for distribution, but that is a different process from ordinary Python execution and does not necessarily translate the whole application into native machine code.

Can Python run without creating .pyc files?#

Yes. .pyc files are optional caches. Python can compile source code when required without saving a bytecode cache to disk.

Is Python bytecode the same on every operating system?#

Do not assume so. CPython bytecode is an implementation detail and is not guaranteed to remain stable across Python versions or work across different Python virtual machines.

Is PyPy compiled or interpreted?#

PyPy is a Python implementation that uses interpretation together with a tracing JIT compiler. The JIT can compile frequently executed paths into machine code while the program runs. Its internal execution model differs from CPython's.

Key takeaway#

Python is commonly called an interpreted language, but CPython does not simply execute raw source text directly. It compiles source into bytecode, then its virtual machine executes that bytecode. Imported modules may have cached bytecode in .pyc files, and other implementations—such as PyPy—can use additional techniques like JIT compilation. Always distinguish the Python language from the implementation executing it.

Related questions

Explore other topics:Forward Deployed EngineeringMachine LearningSQL: Structured Query LanguageAI System Design