When you write and run a Python program, the Python interpreter converts your source code into bytecode — a lower-level, platform-independent representation that runs faster.
This process produces two key file types:
.py→ Python source code file.pyc→ Compiled bytecode file
Let’s understand the difference clearly 👇
🔹 Key Difference Between .py and .pyc Files
| Feature | .py File | .pyc File |
|---|---|---|
| Full Form | Python Source File | Python Compiled File |
| Contains | Human-readable source code written by the developer | Bytecode — compiled, non-human-readable version of the source code |
| Generated By | Written manually by the programmer | Automatically generated by the Python interpreter |
| Purpose | Used to write and edit Python programs | Used to speed up execution (Python loads bytecode instead of recompiling) |
| Readability | Readable and editable | Not human-readable (binary format) |
| Execution | When executed, it’s first compiled to bytecode | Directly executed by the Python Virtual Machine (PVM) |
| Creation Time | Created when you write code (file.py) | Created when you run a .py file for the first time |
| Location | Stored in your project directory | Stored in __pycache__ folder by default |
| Extension Meaning | .py → Python Script | .pyc → Python Compiled |
| Example | script.py | __pycache__/script.cpython-311.pyc |
| Usage | For development | For runtime optimization |
🧩 Example
Step 1: Create a Python File
Create a file named example.py:
def greet():
print("Hello, Dheeraj!")
greet()Step 2: Run the File
When you execute:
python example.pyPython will:
- Compile
example.py→ to bytecode. - Save the compiled version as
example.cpython-311.pyc(inside__pycache__). - Execute the bytecode with the Python Virtual Machine (PVM).
🧩 Step 3: Generated Folder
After running, you’ll see:
project/
│
├── example.py
└── __pycache__/
└── example.cpython-311.pycThe .pyc file contains compiled bytecode that Python reuses for faster loading next time.
🔹 What Is Bytecode?
- Bytecode is an intermediate representation of your Python code.
- It’s not machine code — it’s a low-level, platform-independent code executed by the Python Virtual Machine (PVM).
🧠 Think of it like:
.py → (compiled by Python) → .pyc → executed by Python Virtual Machine🔹 When Are .pyc Files Used?
- When you import a module, Python first looks for a
.pycfile inside__pycache__. - If found and up to date, it loads the
.pycfile directly — skipping recompilation. - If not found, Python recompiles the
.pyfile into.pycand caches it.
🧩 Example: Import Behavior
file1.py
def hello():
print("Hello from file1!")main.py
import file1
file1.hello()When you run:
python main.pyPython will:
- Compile
file1.py→__pycache__/file1.cpython-311.pyc - Next time you import
file1, it uses the cached.pycfile directly.
🔹 Deleting .pyc Files
You can safely delete .pyc files — Python will regenerate them automatically next time the module runs.
To remove all .pyc files:
find . -name "*.pyc" -delete # Linux / macOSor
del /s *.pyc # Windows🔹 Can You Run a .pyc File Directly?
Normally, no — .pyc files are not meant to be run manually.
However, you can run them using:
python __pycache__/example.cpython-311.pycBut usually, you run the .py file, and Python handles .pyc automatically.
🔹 Advantages of .pyc Files
✅ Faster Execution:
Python skips recompilation and directly executes bytecode.
✅ Portability:
Same .pyc file can run on any system with a compatible Python version.
✅ Efficiency in Imports:
Modules load faster when imported multiple times.
🔹 Disadvantages of .pyc Files
⚠️ Not Human Readable:
Cannot be easily edited or understood.
⚠️ Version Dependent:
A .pyc file generated in Python 3.9 may not work with Python 3.11.
⚠️ Automatically Overwritten:
Regenerated when the source .py file changes.
🧾 Summary Table
| Feature | .py File | .pyc File |
|---|---|---|
| Purpose | Source code | Compiled bytecode |
| Readable? | Yes | No |
| Generated By | Programmer | Python interpreter |
| Execution | Compiled at runtime | Executed directly by PVM |
| Location | Project directory | __pycache__ folder |
| Performance | Slightly slower | Faster loading |
| Editable? | Yes | No |
| Dependency | Required to write code | Optional (generated automatically) |
✅ In short:
.py= Your source code (you write it)..pyc= Python’s compiled version (Python creates it for faster execution).- Python stores
.pycfiles inside__pycache__and uses them to speed up imports and runtime performance.
