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:

  • .pyPython source code file
  • .pycCompiled bytecode file

Let’s understand the difference clearly 👇


🔹 Key Difference Between .py and .pyc Files

Feature.py File.pyc File
Full FormPython Source FilePython Compiled File
ContainsHuman-readable source code written by the developerBytecode — compiled, non-human-readable version of the source code
Generated ByWritten manually by the programmerAutomatically generated by the Python interpreter
PurposeUsed to write and edit Python programsUsed to speed up execution (Python loads bytecode instead of recompiling)
ReadabilityReadable and editableNot human-readable (binary format)
ExecutionWhen executed, it’s first compiled to bytecodeDirectly executed by the Python Virtual Machine (PVM)
Creation TimeCreated when you write code (file.py)Created when you run a .py file for the first time
LocationStored in your project directoryStored in __pycache__ folder by default
Extension Meaning.py → Python Script.pyc → Python Compiled
Examplescript.py__pycache__/script.cpython-311.pyc
UsageFor developmentFor 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.py

Python will:

  1. Compile example.py → to bytecode.
  2. Save the compiled version as example.cpython-311.pyc (inside __pycache__).
  3. 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.pyc

The .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 .pyc file inside __pycache__.
  • If found and up to date, it loads the .pyc file directly — skipping recompilation.
  • If not found, Python recompiles the .py file into .pyc and caches it.

🧩 Example: Import Behavior

file1.py

def hello():
    print("Hello from file1!")

main.py

import file1
file1.hello()

When you run:

python main.py

Python will:

  • Compile file1.py__pycache__/file1.cpython-311.pyc
  • Next time you import file1, it uses the cached .pyc file 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 / macOS

or

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

But 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
PurposeSource codeCompiled bytecode
Readable?YesNo
Generated ByProgrammerPython interpreter
ExecutionCompiled at runtimeExecuted directly by PVM
LocationProject directory__pycache__ folder
PerformanceSlightly slowerFaster loading
Editable?YesNo
DependencyRequired to write codeOptional (generated automatically)

In short:

  • .py = Your source code (you write it).
  • .pyc = Python’s compiled version (Python creates it for faster execution).
  • Python stores .pyc files inside __pycache__ and uses them to speed up imports and runtime performance.

Scroll to Top