
Hello everyone, I am Programmer Wan Feng.
Recently, with the rise of AI programming, the popularity of Python has decreased, and I finally have time to focus on the Python topics I have always been interested in.
My exploration of Python is mainly based on two books:
、
The deeper I understand Python’s advanced syntax, the more I can appreciate the intricacies of its design.
Today, let’s talk about the various file types in the Python world.
“
As a Python developer, you must often deal with
<span>.py</span>files. However, there are many other important file types in the Python ecosystem, each with its unique purpose.
📁 Core Python File Types
| File Type | Main Purpose | Readable | Generation Method |
|---|---|---|---|
| .py | Python Source Code | ✅ Yes | Manually Created |
| .pyc | Compiled Bytecode | ❌ No | Automatically Generated by Python |
| .pyo | Optimized Bytecode | ❌ No | Generated by Python with -O flag |
| .pyd | Windows Dynamic Link Library | ❌ No | Compiled from Cython/C Extensions |
| .so | Linux/Mac Dynamic Library | ❌ No | Compiled from Cython/C Extensions |
| .pyw | Console-less Python Script | ✅ Yes | Manually Created |
| .pyx | Cython Source Code | ✅ Yes | Manually Created |
There are some file types you may not have encountered for two reasons:
- You may not have such complex work scenarios, such as the use of .so files.
- Some types are automatically hidden by editors, such as .pyc files.
🔧 Detailed Analysis
1. .py – Python Source Code File
This is the most common Python file, containing human-readable Python code.
# hello.py
def greet(name):
return f"Hello, {name}!"
if __name__ == "__main__":
print(greet("Python Developer: Programmer Wan Feng"))
2. .pyc – Compiled Bytecode File
The Python interpreter compiles .py files into bytecode to speed up subsequent execution.
Generation Method:
# Python automatically generates .pyc files in the __pycache__ directory
python -m py_compile hello.py
File Structure:
- Located in
<span>__pycache__</span>directory - Naming format:
<span>hello.cpython-39.pyc</span> - Contains Python bytecode, not machine code
3. .pyd – Windows Dynamic Link Library
<span>.pyd</span> files are essentially DLL files but can be directly imported by Python.
“
This type can be understood as the jar package format in Java, which can be packaged and shared with others.
Creation Example (using Cython):
# Install Cython
pip install cython
# Create Cython file
# hello.pyx
def cython_greet(name):
return f"Hello from Cython, {name}!"
# setup.py
from setuptools import setup
from Cython.Build import cythonize
setup(ext_modules=cythonize("hello.pyx"))
# Compile to generate .pyd
python setup.py build_ext --inplace
Using .pyd File:
# Import like a regular module
import hello
print(hello.cython_greet("Programmer Wan Feng"))
4. .pyx – Cython Source Code File
Cython is a superset of Python that allows writing C extensions.
# fastmath.pyx
def fibonacci(int n):
cdef int i
cdef double a = 0.0, b = 1.0
for i in range(n):
a, b = b, a + b
return a
5. .pyw – Console-less Script
On Windows, <span>.pyw</span> files do not display a console window when run, making them suitable for GUI applications.
# my_app.pyw
import tkinter as tk
root = tk.Tk()
root.title("Console-less Application")
root.mainloop()
📦 Package and Distribution Files
Package Related Files
| File Type | Purpose |
|---|---|
| __init__.py | Package initialization file (optional in Python 3.3+) |
| main.py | Entry point when the package is executed as a script |
Distribution and Installation
| File Type | Purpose |
|---|---|
| .whl | Distribution format for Python packages (wheel) |
| .egg | Legacy package distribution format |
| setup.py | Package installation script |
| setup.cfg | Package configuration |
| pyproject.toml | Modern package configuration |
| requirements.txt | Dependency list |
🛠️ Configuration File Types
Project Configuration
# pyproject.toml (modern Python project)
[build-system]
requires = ["setuptools", "wheel"]
[project]
name = "my-project"
version = "0.1.0"
# setup.cfg (traditional configuration)
[metadata]
name = my-project
version = 0.1.0
# requirements.txt
requests>=2.25.0
pandas>=1.3.0
Environment and Tool Configuration
| File Type | Purpose |
|---|---|
| .python-version | pyenv version file |
| Pipfile | pipenv dependency management |
| Pipfile.lock | Dependency lock file |
| environment.yml | conda environment configuration |
🔍 Special Purpose Files
1. .pyi – Stub Files
Used for type hints, does not contain implementation code.
# math.pyi
def sqrt(x: float) -> float: ...
def pow(x: float, y: float) -> float: ...
2. .pth – Path Configuration Files
Add custom paths to the Python path.
# my_paths.pth
/home/user/my_python_libs
../relative/path/to/modules
3. .pyz – Self-contained Applications
Zip applications containing all dependencies.
# Create .pyz file
python -m zipapp my_app -o app.pyz
# Run
python app.pyz
💻 Development Tool Files
Testing Related
# test_example.py
import pytest
def test_addition():
assert 1 + 1 == 2
# conftest.py (pytest configuration)
import pytest
@pytest.fixture
def sample_data():
return {"key": "value"}
Code Quality
| File Type | Purpose |
|---|---|
| .pylintrc | Pylint configuration |
| .flake8 | Flake8 configuration |
| .coveragerc | Test coverage configuration |
| .pre-commit-config.yaml | Git hook configuration |
🎯 Real Project Example
A typical Python project structure:
my_project/
├── src/
│ ├── __init__.py
│ ├── main.py
│ └── utils.py
├── tests/
│ ├── __init__.py
│ └── test_main.py
├── docs/
│ └── conf.py
├── .python-version
├── pyproject.toml
├── requirements.txt
├── setup.py
└── README.md
💡 Practical Tips
1. View .pyc File Contents
python -m dis hello.pyc
2. Compile Python Package to .pyd
# Use Cython to compile in bulk
from Cython.Build import cythonize
from setuptools import setup, Extension
extensions = [
Extension("my_module", ["my_module.pyx"])
]
setup(ext_modules=cythonize(extensions))
3. Create Professional Distribution Package
# setup.py
from setuptools import setup, find_packages
setup(
name="my-package",
version="1.0.0",
packages=find_packages(),
install_requires=[
"requests>=2.25.0",
],
entry_points={
'console_scripts': [
'my-command=my_package.cli:main',
],
},
)
🚀 Performance Comparison: .py vs .pyd
In certain scenarios, compiling to .pyd can significantly improve performance:
# Performance test example
import timeit
# Python version
def python_fib(n):
if n <= 1:
return n
return python_fib(n-1) + python_fib(n-2)
# Cython compiled version (assuming compiled to .pyd)
from cython_fib import cython_fib
# Test performance
n = 35
python_time = timeit.timeit(lambda: python_fib(n), number=1)
cython_time = timeit.timeit(lambda: cython_fib(n), number=1)
print(f"Python: {python_time:.2f}s")
print(f"Cython: {cython_time:.2f}s")
print(f"Speedup: {python_time/cython_time:.1f}x")
📊 Summary
The Python file ecosystem is very rich, from source code to compiled files, from configuration to distribution, each file type has its specific purpose:
- Development Stage: Mainly using
<span>.py</span>and<span>.pyx</span> - Runtime Stage: Involves
<span>.pyc</span>,<span>.pyd</span>, and<span>.so</span> - Distribution Stage: Uses
<span>.whl</span>and<span>.egg</span> - Configuration Management: Various configuration files
Mastering the characteristics and uses of these file types can help you better organize projects, optimize performance, and manage dependencies.
Interactive Topic: What other special Python file types have you encountered in your projects? Feel free to share your experiences in the comments!
