Python turns your code into bytecode first, then a tiny virtual machine runs it. See it with dis, meet .pyc files, and find out what the GIL is.
When you run python file.py, does Python read your code line by line and act on each line right away? Not quite.
Here is the key idea. Python is also a compiler. It first turns your code into a simpler list of tiny instructions, and then a small program (a "virtual machine") runs that list. You can even look at the list, and we will.
Your computer's chip only understands very basic instructions, like "add these two numbers". It has no idea what for x in names: means. Something has to translate.
Some languages translate everything once, ahead of time, into chip instructions. Python takes two steps instead.
So "interpreted" is only half the story. Let's take step one.
Python first runs a compiler. That's a translator that reads your whole file and rewrites it. The result is bytecode, which the glossary calls "the internal representation of a Python program in the CPython interpreter".
Bytecode is a to-do list written in very simple steps. Each step is one tiny action, like "load this variable" or "call this function". (CPython is just the name of the standard Python, the one you download from python.org.)
So who follows the checklist?
A virtual machine is, in the docs' words, "a computer defined entirely in software". It is a program that reads your checklist and does each step, one after another.
The CPython internals notes describe it as a loop that goes over the bytecode instructions and runs each one. They also say it is a stack machine. A stack is like a pile of plates: you put things on top, and you take things off the top.
Now let's look at a real checklist.
Yes. Python ships a module called dis (short for disassemble). Try this in a file or in the Python prompt:
import dis
def myfunc(alist):
return len(alist)
dis.dis(myfunc)
This is the example from the dis docs. On the latest version of the docs, the output looks like this:
2 RESUME 0
3 LOAD_GLOBAL 1 (len + NULL)
LOAD_FAST_BORROW 0 (alist)
CALL 1
RETURN_VALUE
The docs say the "2" is a line number. Names may differ on your version, so here is a warning.
Each line is one instruction. Here's what the docs say each one does.
| Instruction | What it does |
|---|---|
RESUME | A no-op. It does internal checks and nothing else you need to care about. |
LOAD_GLOBAL | Loads a global name (here len) onto the stack. |
LOAD_FAST_BORROW | Puts a local variable (here alist) on the stack. |
CALL | Calls a function with the given number of arguments (here 1). |
RETURN_VALUE | Hands the top of the stack back to whoever called. |
Your one line became four tiny steps. Click Next and run them like the virtual machine would.
Put len on the stack
Nothing here knows what len means. It just pushes, calls and returns. That is the whole job.
Compiling takes a bit of time, so Python saves the bytecode to avoid redoing it. These saved files end in .pyc and live in a folder called __pycache__.
The tutorial gives the example __pycache__/spam.cpython-33.pyc for spam.py. The name includes the Python version, so different versions don't trip over each other.
But there is a catch. A .pyc doesn't make the program run faster. The docs say "the only thing that's faster about .pyc files is the speed with which they are loaded". And the file you run directly from the command line is always recompiled, with the result not stored. Only the modules you import get cached.
The glossary states interpreted languages "generally also run more slowly". The reason is that the virtual machine does extra work for every single tiny step, while a chip running ready-made machine code just runs.
The CPython internals notes add that newer versions have a "specializing adaptive interpreter", which speeds things up "by rewriting instructions based on runtime information". So Python keeps getting faster, even though the idea stays the same.
GIL stands for global interpreter lock. The glossary states it is the mechanism CPython uses "to assure that only one thread executes Python bytecode at a time". A thread is one stream of work running inside your program.
Picture a kitchen with a single stove. Many cooks can wait in line, but only one pot heats at a time.
The glossary states the GIL is always released while doing I/O (waiting on files or the network), and some extension modules release it for heavy jobs like compression or hashing. It also states that as of Python 3.13 the GIL can be disabled with a --disable-gil build option.
dis.dis() on a loop or an if and spot the jump instructions.threading versus multiprocessing and see what the GIL changes for each.