✅1.Code is read as text
Firstly your .py code is read line by line as plain text. Example:
This is just a plain text. On running it, the Python interpreter reads the entire
source code line by line as text. The logic isn't yet understood by interpreter,
its just reading characters. This stage prepares your code to be
tokenized and parsed.
---
🧱Step 2: Lexical Analysis- Tokenization
Python now scans your code and breaks into small tokens-the smallest
elements of program. For example:
gets tokenized as:
a
→ identifier =
→ assignment operator5
and 10
→ numeric literals+
→ arithmetic operatorIf there's any basic error in the syntax, python throws a SyntaxError at this stage.
---
🌳 Step 3: Parsing — Building a Syntax Tree
After tokenization, Python begins parsing.
It analyzes the tokens to see if they follow Python's grammar rules — such as whether expressions are valid, blocks are properly indented, loops are well-structured, etc.
At this stage, Python prepares AST(Abstract Syntax Tree)- a tree like representation that shows how a code is structured.
This structure is essential for the interpreter to understand what operations to perform in a proper order.
---
🧮 Step 4: Compilation to Bytecode
Next , The AST is converted to bytecode- an intermediate code that's not human readable.
Pros of Bytecode:
1.It's portable
2. It's execution is faster
3. Stored in .pyc files in the __pycache__ folder.
So when you run a script the second time, Python often skips parsing and uses the compiled bytecode to save time.
---
🔁 Step 5: Execution by Python Virtual Machine (PVM)
Now, the actual execution begins.
Python uses a component- Python Virtual Machine which is just like Java Virtual Machine to interpret the bytecode line wise.
PYTHON VIRTUAL MACHINE:
1. Generates memory for variables
2. Executes operations
3. Manages function calls, loops, recursions
4. Handles exceptions and i/o.
This is where Python acyually "runs" the code. PVM ensures that instructions are carried out in the corerct order, and resulted as expected.
NOTE: PVM can be called as 'Python's engine'.
---
💻Jupyter v/s VS CODE
If using an IDE(Integrated Development Environment) like Jupyter and VS Code , the same process is carried out in the background- but the interface is more user freindly.
These Environments:
1. Automatically handles the interpreter
2. Show you output line
3. Add syntax highlighting and live error detection.
Syntax Highlighting:
Live Error Detection:
(The extra parenthesis at the end of line 1 is highlighted by 'Red' , also shown in title bar)
No Matter which IDE you use, your code is still going through : tokenization > parsing > bytecode > execution.
---
⚠️Common Errors During Execution
This Table shows a list of commons errors that occur while compiling a python code during various stages:
Stage | Error Example | What It Means |
---|---|---|
Tokenizing | SyntaxError: unexpected indent |
Bad whitespace or missing punctuation |
Parsing | SyntaxError: invalid syntax |
Code structure is wrong |
Execution | NameError , ZeroDivisionError |
Runtime issues during bytecode run |
Import | ModuleNotFoundError |
Library missing or not installed |
🧠Why to know the compilation process?
Knowing what happens on running the python code helps you by following ways:
1.Debug smarter (you’ll know where things are going wrong)
2.Appreciate how Python works under the hood
3.Understand how Python is different from compiled languages like C or Java
---
📝 Conclusion
When you run a python code again, remember that it goes through a multi-step route:
1.Reading code as text
2.Break it into tokens
3.Parse into structure
4.Compile into Bytecode
5.Execution by the Python Virtual Machine
The above mentioned simple flow acts like a backbone of every Python application- from your starting projects to full-stack AI models!
---
✨Closing up!
Thanks for reading. I hope you found this post helpful, feel free to:
Share with friends
Leave a comment
Explore more content on CodeCrafted
---
0 Comments