Module 14 · Python Modules and Packages — topic 2 of 13
Creating modules
Writing your own modules: putting functions, classes and constants in a file, the if __name__ == "__main__" guard, private helpers with underscores, __all__, and module documentation.
Learning objectives
- Write a reusable module
- Use the __main__ guard correctly
- Control a module's public interface with naming and __all__
Definition
Creating a module means writing a .py file whose top-level functions, classes and constants are meant to be imported by other code. Code that should only run when the file is executed directly goes under if __name__ == "__main__":.
Introduction
Once you've written a useful function twice, it's time to put it in a module. A well-written module can be imported anywhere and also run on its own for testing or demos.
Why it matters
Good modules are the building blocks of larger programs: easy to import, free of surprising side effects, and clear about what they offer.
Detailed explanation
Put definitions at the top level: functions, classes and constants (UPPER_CASE). Importing then makes them available as module.name.
Avoid code that does work at import time (printing, reading files, network calls). Importing should be fast and silent.
Code under if __name__ == "__main__": runs only when the file is executed directly (python tool.py), not when it is imported — ideal for demos, quick tests and command-line entry points.
Names starting with an underscore (_helper) are internal by convention and are not imported by from module import *.
__all__ = ["name1", "name2"] lists the public names exported by from module import *.
A module docstring and function docstrings document the interface; help(module) displays them.
Keep the module importable from where it will be used: in the same folder as the main script, or inside a package (see Packages).
Syntax
- Top-level definitions become the module's interface.
- __main__ guard for run-only code.
- _names and __all__ control what is public.
Code examples
A module of helpers
49 5.0
The __main__ guard
imported, __name__ is tool -> 10 running tool.py directly 42
More examples
Public and private names
12.57 12 False False cm Area helpers for simple shapes. sizes must be non-negative
Step-by-step explanation
- from shapes import * imports only the names in __all__.
- UNITS and _check are not star-imported but are still reachable as shapes.UNITS and shapes._check.
- The private helper validates inputs for both public functions.
Expected output
Running “Public and private names” prints exactly:
Practical applications
- A utils.py module shared by several scripts.
- Command-line tools whose functions are also importable.
Best practices
- Keep import side-effect free.
- Use the __main__ guard for demos and CLIs.
- Document the module and mark internals with _.
Common mistakes
Work at import time
Avoid
Prefer
Importing the module would be slow and print unexpectedly.
Important notes
- python -m module_name runs a module as a script with __name__ set to "__main__".
Key takeaways
- A module is a file of reusable definitions.
- __main__ guard separates running from importing.
- _names and __all__ define the public interface.
Try it yourself
- 1Create a validators.py module with is_email and is_phone and a __main__ demo.
- 2Add __all__ to a module and check what from module import * brings in.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- ModerateCode under if __name__ == "__main__": runs when…