|
| 1 | +# 📦 Python Packages Guide |
| 2 | + |
| 3 | +> Beginner-friendly documentation about Python packages, imports, modules, and project organization. |
| 4 | +
|
| 5 | +This document explains: |
| 6 | +- what packages are |
| 7 | +- how imports work |
| 8 | +- `__init__.py` |
| 9 | +- package organization |
| 10 | +- absolute vs relative imports |
| 11 | +- common beginner mistakes |
| 12 | +- project structure examples |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +# 📚 Table of Contents |
| 17 | + |
| 18 | +- [📖 What is a Package?](#-what-is-a-package) |
| 19 | +- [📦 What is a Module?](#-what-is-a-module) |
| 20 | +- [🧱 Basic Package Structure](#-basic-package-structure) |
| 21 | +- [📥 Importing Modules](#-importing-modules) |
| 22 | +- [📌 __init__.py](#-__init__py) |
| 23 | +- [🌿 Absolute Imports](#-absolute-imports) |
| 24 | +- [🔗 Relative Imports](#-relative-imports) |
| 25 | +- [🛠️ Organizing Large Projects](#️-organizing-large-projects) |
| 26 | +- [📌 __init__.py Contents](#-__init__py-contents) |
| 27 | +- [⚠️ Common Beginner Mistakes](#️-common-beginner-mistakes) |
| 28 | +- [📚 Final Notes](#-final-notes) |
| 29 | + |
| 30 | +--- |
| 31 | + |
| 32 | +# 📖 What is a Package? |
| 33 | + |
| 34 | +A package is a folder containing Python modules. |
| 35 | + |
| 36 | +Packages help: |
| 37 | +- organize code |
| 38 | +- separate logic |
| 39 | +- structure large projects |
| 40 | +- improve readability |
| 41 | + |
| 42 | +--- |
| 43 | + |
| 44 | +# 📦 What is a Module? |
| 45 | + |
| 46 | +A module is simply: |
| 47 | + |
| 48 | +```text |
| 49 | +a Python file |
| 50 | +``` |
| 51 | + |
| 52 | +Example: |
| 53 | + |
| 54 | +```text |
| 55 | +utils.py |
| 56 | +``` |
| 57 | + |
| 58 | +This file itself is a module. |
| 59 | + |
| 60 | +--- |
| 61 | + |
| 62 | +# 🧱 Basic Package Structure |
| 63 | + |
| 64 | +```text |
| 65 | +project/ |
| 66 | +│ |
| 67 | +├── main.py |
| 68 | +├── parser/ |
| 69 | +│ ├── __init__.py |
| 70 | +│ ├── config_parser.py |
| 71 | +│ └── validator.py |
| 72 | +│ |
| 73 | +└── render/ |
| 74 | + ├── __init__.py |
| 75 | + └── draw.py |
| 76 | +``` |
| 77 | + |
| 78 | +--- |
| 79 | + |
| 80 | +# 📥 Importing Modules |
| 81 | + |
| 82 | +## Import Entire Module |
| 83 | + |
| 84 | +```python |
| 85 | +import math |
| 86 | +``` |
| 87 | + |
| 88 | +Usage: |
| 89 | + |
| 90 | +```python |
| 91 | +math.sqrt(25) |
| 92 | +``` |
| 93 | + |
| 94 | +--- |
| 95 | + |
| 96 | +## Import Specific Function |
| 97 | + |
| 98 | +```python |
| 99 | +from math import sqrt |
| 100 | +``` |
| 101 | + |
| 102 | +Usage: |
| 103 | + |
| 104 | +```python |
| 105 | +sqrt(25) |
| 106 | +``` |
| 107 | + |
| 108 | +--- |
| 109 | + |
| 110 | +# 📌 __init__.py |
| 111 | + |
| 112 | +`__init__.py` tells Python that a folder should behave like a package. |
| 113 | + |
| 114 | +Without it, imports may fail in some project structures. |
| 115 | + |
| 116 | +--- |
| 117 | + |
| 118 | +# 🌿 Absolute Imports |
| 119 | + |
| 120 | +Absolute imports start from the project root. |
| 121 | + |
| 122 | +## Example |
| 123 | + |
| 124 | +```python |
| 125 | +from parser.config_parser import parse_config |
| 126 | +``` |
| 127 | + |
| 128 | +--- |
| 129 | + |
| 130 | +# 🔗 Relative Imports |
| 131 | + |
| 132 | +Relative imports use dots (`.`). |
| 133 | + |
| 134 | +## Example |
| 135 | + |
| 136 | +```python |
| 137 | +from .validator import validate_config |
| 138 | +``` |
| 139 | + |
| 140 | +--- |
| 141 | + |
| 142 | +# 🛠️ Organizing Large Projects |
| 143 | + |
| 144 | +Example organization: |
| 145 | + |
| 146 | +```text |
| 147 | +project/ |
| 148 | +│ |
| 149 | +├── core/ |
| 150 | +├── parsing/ |
| 151 | +├── render/ |
| 152 | +├── utils/ |
| 153 | +├── tests/ |
| 154 | +└── assets/ |
| 155 | +└── main.py |
| 156 | +``` |
| 157 | + |
| 158 | +And each folder would have a __init__.py file to make it a package. |
| 159 | + |
| 160 | +--- |
| 161 | + |
| 162 | +# 📌 __init__.py Contents |
| 163 | + |
| 164 | +In many Python projects, `__init__.py` is often left empty. |
| 165 | + |
| 166 | +Example: |
| 167 | + |
| 168 | +```python |
| 169 | +# __init__.py |
| 170 | +``` |
| 171 | + |
| 172 | +This is completely normal and commonly used only to mark the folder as a package. |
| 173 | + |
| 174 | +--- |
| 175 | + |
| 176 | +# Why Leave It Empty? |
| 177 | + |
| 178 | +An empty `__init__.py`: |
| 179 | +- keeps the package simple |
| 180 | +- avoids unnecessary imports |
| 181 | +- helps Python recognize the directory as a package |
| 182 | + |
| 183 | +--- |
| 184 | + |
| 185 | +# What Can Be Inside __init__.py? |
| 186 | + |
| 187 | +Although often empty, it may also contain: |
| 188 | +- package imports |
| 189 | +- helper variables |
| 190 | +- package initialization code |
| 191 | +- exposed public functions/classes |
| 192 | + |
| 193 | +--- |
| 194 | + |
| 195 | +# Example |
| 196 | + |
| 197 | +```python |
| 198 | +from .config_parser import parse_config |
| 199 | +from .validator import validate_config |
| 200 | +``` |
| 201 | + |
| 202 | +This allows imports like: |
| 203 | + |
| 204 | +```python |
| 205 | +from parser import parse_config |
| 206 | +``` |
| 207 | + |
| 208 | +instead of: |
| 209 | + |
| 210 | +```python |
| 211 | +from parser.config_parser import parse_config |
| 212 | +``` |
| 213 | + |
| 214 | +--- |
| 215 | + |
| 216 | +# Why This is Useful |
| 217 | + |
| 218 | +This can: |
| 219 | +- simplify imports |
| 220 | +- expose public APIs |
| 221 | +- improve package organization |
| 222 | + |
| 223 | +Very common in: |
| 224 | +- libraries |
| 225 | +- frameworks |
| 226 | +- large Python projects |
| 227 | + |
| 228 | +--- |
| 229 | + |
| 230 | +# Good Practice |
| 231 | + |
| 232 | +For beginner projects: |
| 233 | +- keeping `__init__.py` empty is completely fine |
| 234 | + |
| 235 | +As projects grow larger: |
| 236 | +- it may be used to organize package exports and initialization logic |
| 237 | + |
| 238 | + |
| 239 | +--- |
| 240 | + |
| 241 | +# ⚠️ Common Beginner Mistakes |
| 242 | + |
| 243 | +## ❌ Missing __init__.py |
| 244 | + |
| 245 | +Problem: |
| 246 | + |
| 247 | +```text |
| 248 | +ModuleNotFoundError |
| 249 | +``` |
| 250 | + |
| 251 | +--- |
| 252 | + |
| 253 | +## ❌ Circular Imports |
| 254 | + |
| 255 | +Example: |
| 256 | + |
| 257 | +```python |
| 258 | +# a.py imports b.py |
| 259 | +# b.py imports a.py |
| 260 | +``` |
| 261 | + |
| 262 | +--- |
| 263 | + |
| 264 | +## ❌ Running Files Directly |
| 265 | + |
| 266 | +Bad: |
| 267 | + |
| 268 | +```bash |
| 269 | +python3 parser/config_parser.py |
| 270 | +``` |
| 271 | + |
| 272 | +Better: |
| 273 | + |
| 274 | +```bash |
| 275 | +python3 main.py |
| 276 | +``` |
| 277 | + |
| 278 | + |
| 279 | +--- |
| 280 | + |
| 281 | +# 📚 Final Notes |
| 282 | + |
| 283 | +Understanding packages is extremely important for modern Python development. |
| 284 | + |
| 285 | +Good package organization helps: |
| 286 | +- readability |
| 287 | +- maintainability |
| 288 | +- debugging |
| 289 | +- scalability |
| 290 | +- teamwork |
| 291 | + |
| 292 | +--- |
0 commit comments