UcenlyOpen in the app

Programming Essentials · BE5B33PRG · Topic 5 of 9

Modules, namespaces and imports

Organize code by responsibility and control import-time behavior.

Open the interactive lesson

Key ideas

Module

A .py file is a module. import math makes math.sqrt available through the module name.

Namespace

A namespace maps names to objects. The course covers local, global (module) and built-in scopes; the innermost one takes precedence. (Python also has enclosing-function scopes, the LEGB rule, which goes beyond the lectures.)

Reusable file

Put a demonstration under if __name__ == "__main__": so importing the module does not run it.

In one line: Keep library functions importable without surprise side effects.

Sources

Written by Ucenly. Based on the BE5B33PRG course materials: