Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What do the python file extensions, .pyc .pyd .pyo stand for?
Python uses different file extensions to distinguish between various types of files in the Python ecosystem. Understanding these extensions helps developers work more effectively with Python code, modules, and compiled bytecode.
Python File Extensions Overview
The most common Python file extensions and their purposes are ?
- .py: Standard Python source code files
- .pyc: Compiled bytecode files for faster loading
- .pyo: Optimized bytecode files (deprecated in Python 3.5+)
- .pyd: Python extension modules on Windows (similar to .dll files)
- .pyw: Windows GUI scripts that run without a console window
.py Files
The .py extension identifies standard Python source code files. These contain human-readable Python code that gets interpreted or compiled when executed.
Example
# hello.py
def greet(name):
print(f"Hello, {name}!")
greet("Alice")
Hello, Alice!
Execute this file using: python hello.py
.pyc Files
Python automatically creates .pyc files containing compiled bytecode when modules are imported. These files speed up subsequent imports by skipping the compilation step.
When you import a module, Python creates a .pyc file in the __pycache__ directory. You can also manually compile files using ?
python -m compileall filename.py
Example
# math_utils.py
def add_numbers(a, b):
return a + b
def multiply_numbers(a, b):
return a * b
# When imported, this creates math_utils.pyc
import math_utils
result = math_utils.add_numbers(5, 3)
print(result)
8
.pyo Files (Deprecated)
The .pyo extension was used for optimized bytecode files created with the -O flag. Python 3.5+ replaced this system with .opt-1.pyc and .opt-2.pyc files in the __pycache__ directory.
In older Python versions, running python -O script.py would generate optimized bytecode files.
.pyd Files
.pyd files are Windows-specific Python extension modules, similar to .dll files. They contain compiled code (often C/C++) that can be imported as Python modules. On Unix systems, equivalent files use .so extension.
Example
# Example of using a .pyd module (conceptual)
# Assume we have mymath.pyd containing fast math functions
import mymath # This would import the .pyd file
result = mymath.fast_multiply(100, 50)
print(f"Result: {result}")
Result: 5000
File Extension Comparison
| Extension | Type | Purpose | Platform |
|---|---|---|---|
| .py | Source Code | Human-readable Python code | All |
| .pyc | Bytecode | Compiled Python for faster loading | All |
| .pyo | Optimized Bytecode | Deprecated optimized compilation | All (deprecated) |
| .pyd | Extension Module | Compiled extension (like .dll) | Windows |
Conclusion
Python file extensions help organize different types of code files. Use .py for source code, understand that .pyc files improve import performance, and recognize .pyd files as Windows extension modules. The .pyo extension is now deprecated in favor of optimized .pyc files.
