| 1 | CPython (python3) | the C program that compiles and runs your Python | one operating-system process per run | which Python version and which sys.path your code gets | code that works on your laptop and fails on a server with another Python |
|---|
| 2 | import system | finds each module on sys.path and loads it once | the interpreter, through sys.meta_path | which file an import statement actually loads | ModuleNotFoundError, or your own random.py loaded instead of the standard one |
|---|
| 3 | .pyc cache | keeps the bytecode of imported modules for the next start | a __pycache__ folder beside each module | whether a module is recompiled, by comparing the source’s time and size | slow starts in read-only containers that recompile every module every time |
|---|
| 4 | compiler | turns source text into code objects full of bytecode | inside the process, before any line of the file runs | which names become numbered local slots and which stay global lookups | a SyntaxError before anything runs, pointing at the line it couldn’t parse |
|---|
| 5 | evaluation loop | runs bytecode one instruction at a time, each call in its own frame | every thread that runs Python code | nothing about types; it runs each instruction in order | pure-Python loops that take seconds where compiled code takes milliseconds |
|---|
| 6 | objects | hold every value, each with a reference count and a type | the process’s memory, shared by all its threads | when a value is freed: at zero references, or by the cycle collector | memory far above the data’s size, such as a gigabyte of floats in lists |
|---|
| 7 | specializer | rewrites hot instructions for the types they keep seeing | inside the evaluation loop, always on since Python 3.11 | which fast version of an instruction runs, behind a type check | a hot loop slower than expected because its value types keep changing |
|---|
| 8 | JIT | compiles hot loops into machine code while the program runs | beside the evaluation loop, in builds that include it | nothing until it is switched on; then which loops become machine code | PYTHON_JIT=1 changing nothing, because the build has no JIT |
|---|
| 9 | GIL | lets one thread at a time run Python bytecode | the interpreter, shared by all of its threads | which thread may run Python, handing over every 5 ms | four busy threads finishing no faster than one |
|---|
| 10 | C extensions | run compiled C code that Python imports and calls like a module | .so files in site-packages, loaded with dlopen | nothing on their own; they do the work Python asks for | an ImportError about a missing symbol after upgrading Python |
|---|
| 11 | NumPy and BLAS | store numbers raw and loop over them in compiled code | NumPy’s extension, which loads a BLAS library | how each array operation runs, using SIMD and every core | a Python loop over an array running slower than one over a list |
|---|
| 12 | PyTorch | sends each tensor operation to a C++ or GPU kernel | torch._C, an extension that holds the dispatcher | which kernel runs, from the tensor’s device and whether it records gradients | a GPU that sits mostly idle while Python launches tiny operations |
|---|
| 13 | GPU | runs matrix kernels in its own memory, apart from Python | a PCIe device fed through CUDA streams | nothing; it runs kernels in the order they were queued | timings that look instant because nothing waited for the GPU |
|---|
| 14 | other implementations | run the same Python language with a different design | replace CPython as the whole process | how loops are compiled and whether C extensions run at full speed | a package that won’t install, or NumPy code that gets slower on PyPy |
|---|