Use this when organizing Python code into modules.
Modules · Python Automation
Python modules are .py files or packages you import, from the standard library like os and json or third-party packages from PyPI, organizing automation into reusable parts.
1. Modules and packages organise code.
2. Use pyproject.toml.
3. Avoid circular imports.
4. Public API in __init__.
5. Install in editable mode for development.
package → modules → imports
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Verification path Verify boundary
The negative fixture should deliberately exercise risks such as environment drift, implicit coercion, broad exception handling, blocking/async confusion, shell quoting/subprocess risks, partial writes and non-deterministic external APIs, choosing the cases most relevant to module name/namespace/revision, imports/includes, revision selection and capability exposure to clients.
Step 2 Common mistakes Narrow the fault
- Naming a file like a standard module (
json.py,requests.py), shadowing it. - Code with side effects at import time.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- Import standard library and PyPI packages.
- Organize code into packages.
- Avoid circular imports.
python3 -c 'import mymodule; print(mymodule.__file__)'— import pathpython3 -m pip show <pkg>— package info- Package structure (pyproject.toml) — layout
ReferenceWhy it matters1 section · depth on demand
What it is
A module is a .py file you can import; a package is a folder of modules. Split reusable code (API client, parsing) into modules instead of copying functions between scripts.
ReferenceImportant terms & boundaries2 sections · depth on demand
In depth
A module is a Python file that can be imported; a package is a directory of modules. Splitting automation code into modules keeps scripts short, lets functions be reused across tools and makes them testable.
netauto/
__init__.py
inventory.py # load devices from NetBox
render.py # Jinja2 rendering
push.py # Netmiko/NAPALM deployment
scripts/
deploy_vlans.py # from netauto import inventory, render, push
tests/
test_render.py
if __name__ == "__main__":
main() # runs only when executed directly, not when imported
The standard library already covers much automation work (json, csv, ipaddress, pathlib, subprocess, logging, argparse) — check it before adding dependencies.
Example
# lib/netbox.py
def get_devices(session, url): ...
# main.py
from lib.netbox import get_devices
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Python Automation > Modules. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://docs.python.org/3/ · confidence 94%Deep reference
Healthy vs suspicious evidence
Output interpretation
For module name/namespace/revision, imports/includes, revision selection and capability exposure to clients, prefer typed fields and stable IDs over human presentation text.