Python Modules and Imports
Package layout, absolute imports, and the main guard.
- python import
- __main__
- python -m
- package layout
- circular import
Python Modules and Imports
Package layout, absolute imports, and the main guard.
Commands
| Command | What it does |
|---|---|
from pkg.mod import thing | Absolute import (preferred) |
dunder-main guard | Script entry guard |
python -m pkg.mod | Run a module as a script |
init-py marker | Marks a directory as a package |
sys.path via PYTHONPATH | Where imports are resolved |
Example
myapp: init, core, cli modules
FAQ
Relative or absolute imports?
Absolute (from pkg.mod import x) — explicit, refactor-safe, and required by most linters.
Circular import error — fix?
Move shared code to a third module or import inside the function; restructure if it recurs.
Preguntas frecuentes
Relative or absolute imports?
Absolute (from pkg.mod import x) — explicit, refactor-safe, and required by most linters.
Circular import error — fix?
Move shared code to a third module or import inside the function; restructure if it recurs.
Python
Python Venv and Environments
Isolate dependencies per project with venv and pinned requirements.
Python Pip Essentials
Install, upgrade, and audit packages with pip like a pro.
Python Basics
Syntax survival kit: variables, loops, f-strings, and errors.
Python Strings
Slice, split, join, and format text efficiently.