Project Structure and PEP 8 Best Practices in Python
๐ก Intermediate
๐ Definition
- PEP 8: The official Style Guide for Python Code that defines formatting conventions (indentation, naming styles, line length, docstrings) to ensure maximum code readability across the Python community.
- Python Project Structure: Organizing source code, tests, documentation, dependencies, and configuration files into standardized professional directory layouts.
๐ฎ๐ณ Hindi
Clean aur maintainable Python projects banane ke liye PEP 8 Style Guide follow ki jaati hai. 4-space indentation, snake_case variable/function names, PascalCase class names, Docstrings ("""Docstring"""), aur modular project folder structures maintenance ko aasan banate hain.
๐ฉ Marathi
Professional Python projects saathi PEP 8 Style Guide aani standardized directory structure vaparatat.
๐ 1. PEP 8 Core Naming & Formatting Rules
| Code Element | PEP 8 Naming Style | Example |
|---|---|---|
| Variables & Functions | snake_case |
user_age, calculate_total() |
| Classes | PascalCase / CamelCase |
BankAccount, UserProfile |
| Constants | ALL_CAPS |
MAX_CONNECTIONS = 10 |
| Modules & Packages | snake_case (short) |
calculator.py, utils |
| Indentation | 4 Spaces (NO Tabs!) | 4 spaces per block level |
| Line Length | Max 79โ88 characters | Black formatter standard |
๐ 2. Writing Clean Python Docstrings (PEP 257)
Use triple-quoted strings """ ... """ directly under function and class declarations to document intent, parameters, and return types:
def calculate_bmi(weight_kg: float, height_m: float) -> float:
"""Calculates Body Mass Index (BMI).
Args:
weight_kg (float): Weight in kilograms.
height_m (float): Height in meters.
Returns:
float: Calculated BMI value.
"""
if height_m <= 0:
raise ValueError("Height must be positive.")
return weight_kg / (height_m ** 2)
๐ 3. Standard Professional Project Layout
my_python_project/
โโโ .gitignore # Git ignore patterns (venv/, __pycache__/)
โโโ README.md # Project documentation & setup guide
โโโ requirements.txt # Dependency package manifest
โโโ setup.py / pyproject.toml # Package build configuration
โโโ src/ # Source code directory
โ โโโ my_project/
โ โโโ __init__.py # Package marker
โ โโโ main.py # CLI/App Entry point
โ โโโ models.py # Data models / Dataclasses
โ โโโ utils.py # Helper utilities
โโโ tests/ # Unit tests directory
โโโ __init__.py
โโโ test_models.py
๐ก Complete Example: Well-Structured Clean Code Module
# src/my_project/models.py
from dataclasses import dataclass
from typing import Optional
CONSTANT_TAX_RATE = 0.18 # PEP 8 Constant
@dataclass
class InvoiceItem:
"""Represents an individual item in an e-commerce invoice."""
product_name: str
unit_price: float
quantity: int = 1
def calculate_subtotal(self) -> float:
"""Computes subtotal for the item."""
return self.unit_price * self.quantity
def format_currency(amount: float) -> str:
"""Formats floating-point amount as currency string."""
return f"โน{amount:.2f}"
if __name__ == "__main__":
item = InvoiceItem("Wireless Keyboard", 1499.00, 2)
print(f"Item: {item.product_name}")
print(f"Subtotal: {format_currency(item.calculate_subtotal())}")
๐ Output
Item: Wireless Keyboard
Subtotal: โน2998.00
โ ๏ธ Common Mistakes
- Mixing tabs and spaces for indentation, leading to
TabErrorcrashes. - Writing monolithic 1,000-line single script files instead of breaking logic into modular package files.
- Committing compiled bytecode folders (
__pycache__/,.pycfiles) into Git repositories.
๐ก๏ธ Safety / Important Notes
Use automated code linting and formatting tools like black (pip install black) and flake8 (pip install flake8) to auto-format your code to PEP 8 standards before pushing changes.
๐ Real-World Usage
All open-source Python libraries, enterprise backend repositories, and production software teams enforce PEP 8 linting and clean project structures.
๐งช Try It Yourself
- Install
blackusingpip install blackin your virtual environment. - Auto-format a Python file using command
black my_script.py.
๐ฏ Mini Challenge
Re-structure an unformatted Python script by renaming variable names to snake_case, class names to PascalCase, adding 4-space indentation, and adding a function docstring.
๐ Related Topics
๐งญ Navigation
| โ Python Home | โ Previous: Virtual Environments and Pip | Next: Mini Projects โ |