Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Blog/Foundations

What happens when you run Python code?

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.

Derpx0612 Oct · 5 min read
Contents
Why can't the chip just run my Python?What does the compiler produce?What does the virtual machine do?Can I actually see the bytecode?What does each line mean?What are .pyc files?So why is Python "slow"?What is the GIL?What should I learn next?Sources
0% read · 5 min leftBack to top ↑
On this page0%
Contents
Why can't the chip just run my Python?What does the compiler produce?What does the virtual machine do?Can I actually see the bytecode?What does each line mean?What are .pyc files?So why is Python "slow"?What is the GIL?What should I learn next?Sources

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.

Why can't the chip just run my Python?

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.

1Your .py file
2Compiler
3Bytecode
4Virtual machine
5Output
Two steps, not one.

So "interpreted" is only half the story. Let's take step one.

What does the compiler produce?

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.)

A recipe

Your code is a recipe written in paragraphs. Bytecode is the same recipe as a numbered checklist: boil water, add pasta, stir. Each step is easy to follow without thinking.

So who follows the checklist?

What does the virtual machine do?

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.

Fetch instruction
Run that instruction
Move to the next one
↺ repeat
The heart of Python, going round and round.

Now let's look at a real checklist.

Can I actually see the bytecode?

Yes. Python ships a module called dis (short for disassemble). Try this in a file or in the Python prompt:

Python
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:

Text
  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.

Bytecode changes between versions

The docs say bytecode is "an implementation detail of the CPython interpreter" and that no guarantees are made it won't change between versions. So treat dis as a window to peek through, not something to build on.

What does each line mean?

Each line is one instruction. Here's what the docs say each one does.

InstructionWhat it does
RESUMEA no-op. It does internal checks and nothing else you need to care about.
LOAD_GLOBALLoads a global name (here len) onto the stack.
LOAD_FAST_BORROWPuts a local variable (here alist) on the stack.
CALLCalls a function with the given number of arguments (here 1).
RETURN_VALUEHands 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.

LOAD_GLOBAL
LOAD_FAST_BORROW
CALL
RETURN_VALUE

Put len on the stack

Step 1 of 4
One instruction at a time, the same four steps as the table above.

Nothing here knows what len means. It just pushes, calls and returns. That is the whole job.

What are .pyc files?

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.

First importImport a module the first time: compile it, save a .pyc
→
Next importImport it again: load the .pyc, skip the compile
Saves loading time, not running time.

So why is Python "slow"?

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.

What is the GIL?

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.

What should I learn next?

  • Run dis.dis() on a loop or an if and spot the jump instructions.
  • Look at threading versus multiprocessing and see what the GIL changes for each.
Key takeaways
  • python file.py compiles your code to bytecode first, then a virtual machine runs that bytecode.
  • dis lets you see the bytecode, but it can change between Python versions.
  • .pyc files speed up loading imported modules, not running them.
  • The GIL lets only one thread run Python bytecode at a time.

Sources

  • dis module docs, Python documentation
  • Glossary: bytecode, virtual machine, interpreted, global interpreter lock, Python documentation
  • Modules: compiled Python files, Python tutorial
  • CPython interpreter internals, CPython repository
ShareXLinkedInEmail
Written by Derpx06A developer who builds AI search tools and the code around them. I explain what I got wrong first, so you can skip it. Spotted a mistake? Tell me — corrections get credited.
Foundations · 3 minWhy is 0.1 + 0.2 not 0.3 in code?Foundations · 4 minHow does Git work inside? (Snapshots, not changes)Foundations · 5 minHow do maps apps find the fastest route?
Start here
01Why is 0.1 + 0.2 not 0.3 in code?02How does Git work inside? (Snapshots, not changes)03How does your computer know the time? (And why it can be wrong)