|$ curl https://forge-ai.dev/api/markdown?path=docs/python/modules
$cat docs/python-—-modules-&-packages.md
updated Recently·15 min read·published

Python — Modules & Packages

PythonIntermediate
Introduction

Modules and packages are Python's mechanism for organizing code into reusable, namespaced units. Every .py file is a module, and directories with __init__.py become packages.

Import System
imports.py
Python
1# Various import styles
2import math # import whole module
3from math import sqrt # import specific name
4from math import sqrt as sq # import with alias
5from math import * # import everything (avoid!)
6import math as m # module alias
7
8# Using imported items
9print(math.pi) # → 3.14159...
10print(sqrt(16)) # → 4.0
11print(m.floor(3.7)) # → 3
12
13# Import path resolution (in order):
14# 1. sys.modules (already imported)
15# 2. Built-in modules
16# 3. sys.path directories (current dir, PYTHONPATH, site-packages)
17
18# See the module search path:
19import sys
20print(sys.path)
21
22# Module caching — modules are imported only once
23# Subsequent imports get the cached module object
24
25# Reload a module (useful in development)
26import importlib
27importlib.reload(math) # re-execute the module

best practice

Prefer from module import specific_name over importing whole modules when you only need a few items. It makes dependencies explicit. Never use from module import * in production code — it pollutes the namespace.
Creating Modules

Any .py file is a module. Its name is the filename without .py.

greetings.py
Python
1# File: greetings.py
2"""Greetings module — reusable greeting functions."""
3
4def hello(name: str) -> str:
5 return f"Hello, {name}!"
6
7def goodbye(name: str) -> str:
8 return f"Goodbye, {name}!"
9
10PI = 3.14159 # module-level "constant"
11
12# Module-level code runs once on first import
13print(f"Loaded {__name__}") # → Loaded greetings
14
15# The if __name__ guard
16if __name__ == "__main__":
17 # This code runs ONLY when executed directly:
18 # python greetings.py
19 print(hello("World"))
20
21# Using it:
22# import greetings
23# print(greetings.hello("Alice")) # → Hello, Alice!
24
25# __name__ is the module's name:
26# - When imported: "greetings"
27# - When run directly: "__main__"
Packages

Packages are directories containing an __init__.py file (can be empty) and module files. They create hierarchical namespaces.

packages.py
Python
1# Directory structure:
2# mypackage/
3# __init__.py
4# module_a.py
5# module_b.py
6# subpackage/
7# __init__.py
8# module_c.py
9
10# __init__.py controls package initialization and exports
11# mypackage/__init__.py:
12# from .module_a import useful_function
13# __all__ = ["useful_function"] # controls `from pkg import *`
14
15# Importing from packages:
16# from mypackage import module_a
17# from mypackage.module_a import useful_function
18# from mypackage.subpackage import module_c
19
20# Relative imports (inside a package):
21# from . import sibling_module # current package
22# from .module_a import helper # sibling module
23# from .subpackage import module_c # subpackage
24# from .. import parent_module # parent package
25
26# __init__.py can also do:
27# - Package-level imports (convenience API)
28# - Package metadata
29# - Lazy loading
Standard Library Highlights

Python's standard library is extensive. These are the modules every Python developer should know:

stdlib.py
Python
1# os — operating system interface
2import os
3os.getcwd() # current directory
4os.listdir(".") # list files
5os.environ # environment variables (dict-like)
6
7# sys — Python interpreter access
8import sys
9sys.version # Python version string
10sys.argv # command line arguments
11sys.exit(0) # exit with status code
12
13# pathlib — modern filesystem paths (prefer over os.path)
14from pathlib import Path
15p = Path("/tmp/data/file.txt")
16p.parent # → /tmp/data
17p.name # → file.txt
18p.suffix # → .txt
19p.exists() # → bool
20p.read_text() # read file content
21p.write_text("hello") # write file content
22
23# json — JSON encoding/decoding
24import json
25data = {"name": "Alice", "scores": [1, 2, 3]}
26text = json.dumps(data, indent=2) # serialize
27restored = json.loads(text) # deserialize
28
29# re — regular expressions
30import re
31pattern = r"\d{3}-\d{4}" # phone pattern
32match = re.search(pattern, "Call 555-1234")
33if match:
34 print(match.group()) # → 555-1234
35
36# datetime — dates and times
37from datetime import datetime, timedelta
38now = datetime.now()
39future = now + timedelta(days=7)
40formatted = now.strftime("%Y-%m-%d %H:%M")
41
42# collections — specialized data structures
43from collections import defaultdict, Counter, deque
44
45# itertools — iterator tools
46from itertools import chain, product, permutations, groupby
47
48# functools — higher-order functions
49from functools import partial, lru_cache, wraps
50
51# hashlib — cryptographic hashing
52import hashlib
53hashlib.sha256(b"hello").hexdigest()
54
55# uuid — universally unique identifiers
56import uuid
57uuid.uuid4()
58
59# statistics — statistical functions
60from statistics import mean, median, stdev
61
62# dataclasses — automatic __init__, __repr__, etc.
63from dataclasses import dataclass
64
65# typing — type hints
66from typing import Optional, List, Dict, Tuple
Package Management
package_management.txt
Bash
1# pip — Python's package installer
2pip install requests # install a package
3pip install -r requirements.txt # install from file
4pip uninstall requests # remove
5pip list # list installed
6pip freeze > requirements.txt # save current environment
7pip show requests # package details
8
9# requirements.txt format:
10# requests==2.31.0
11# numpy>=1.24,<2.0
12# pytest>=7.0
13
14# Modern alternatives (faster):
15# uv — Rust-based pip replacement
16# uv pip install requests
17# uv pip compile requirements.in > requirements.txt
18
19# Virtual environments (isolate dependencies)
20python3 -m venv .venv # create
21source .venv/bin/activate # activate (Linux/macOS)
22.venv\Scripts\activate # activate (Windows)
23deactivate # deactivate
24
25# Or use:
26# python3 -m venv .venv && source .venv/bin/activate && pip install -r requirements.txt

info

Always use virtual environments for project isolation. Modern tools like uv and rye are significantly faster than pip and worth switching to for new projects.
$Blueprint — Engineering Documentation·Section ID: PYTHON-MOD·Revision: 1.0