← Lesson
BitWithBite
Python · Quick Reference

Lesson 15 — Modules & Packages Cheat Sheet

Python
In one line: A module is simply a Python file. Any .py file can be imported by other files. When you write import math, Python finds math.py (or a compiled equivalent) and executes it, makin...

Key Ideas

1Creating & Importing Modules. A module is simply a Python file. Any .py file can be imported by other files. When you write import math, Python finds math.py (or a compiled equivalent) and executes...
2Packages — Organising Multiple Modules. A package is a folder containing an __init__.py file and multiple modules. This lets you organise large projects into namespaced sub-modules. The __init__.py runs when...
3pip & Virtual Environments. pip is Python's package installer. It downloads and installs packages from PyPI (Python Package Index). A virtual environment is an isolated Python installation for ea...
4Standard Library — Must-Know Modules. Python ships with an enormous standard library — "batteries included." These are the modules every Python developer reaches for regularly:

Code Examples

# mathutils.py — a reusable math utilities module PI = 3.14159265358979 def circle_area(r): """Return area of a circle.""" return PI * r ** 2 def circle_circumference(r): return 2 * PI * r def is_prime(n): """Return True if n is prime.""...
# Style 1: import the whole module (namespace preserved) import mathutils print(mathutils.circle_area(3)) # 28.274... print(mathutils.PI) # 3.14159... # Style 2: import specific names from mathutils import is_prime, PI print(is_prime(13)) ...
# __init__.py — package initialisation # Import key names so users can write: from bwb_toolkit import validate_email from .validators import validate_email, validate_age from .formatters import format_currency, format_date __version__ = "1.0.0" __author__ ...