androidengineers.Book a session

Python 05 · Functions and modules

Modules, imports, and executable entry points

articleSelf-paced

Learn the concept

A module is a Python source file that can be imported. A package groups modules. Imports let you separate responsibilities such as parsing, validation, and reporting. Avoid filenames that shadow standard-library modules, such as naming your file json.py.

Importing a module executes its top-level statements. Network requests and destructive operations should therefore not happen merely because a test imports your code. Put command-line behavior behind a main function and an entry-point guard.

Absolute imports identify the package being used; relative imports refer to nearby modules inside a package. Run modules in the intended package context instead of modifying import paths until something happens to work. Document the supported command from the repository root.

Run and inspect

def main():
    return "Ready to process documents"

if __name__ == "__main__":
    print(main())

assert main().startswith("Ready")

Your exercise

Create parser.py and report.py, import their functions from a small main module, and ensure importing each file produces no output or external action.

Check your understanding

The project runs from its documented entry point and the modules can be imported independently by tests.

YOUR LEARNING JOURNEY

0 of 42 available lessons completed

Progress saved in this browser. No account needed.
Modules, imports, and executable entry points | Python for AI Engineering | Android Engineers