liveBook · Manning

Module

As explained in The Quick Python Book, Fourth Edition

A module is a distinct, bounded software unit that groups related code or functionality and provides an organizational or interaction boundary within a larger system.

A Python module groups related functions, classes, variables, or other objects under one name. Most modules are Python source files: a file named mymath.py defines a module imported as mymath. Modules make reusable code easier to organize and give it a separate namespace, so mymodule.reverse and othermodule.reverse can coexist. section 10.1 section 10.2

Listing 10.1 File mymath.py
"""mymath - our example math module"""
pi = 3.14159
def area(r):
   """area(r): return the area of a circle with radius r."""
   return(pi * r * r) 

The small mymath.py file shows a module containing a value, a function, and documentation strings. section 10.2

Import and call a function

With mymath.py available, `import mymath` lets you call `mymath.area(2)`, which returns `12.56636`, or read `mymath.pi`, which is `3.14159`. The import does not make bare `area(2)` or `pi` available. Use `from mymath import pi` if you want to refer to that value without the module prefix. section 10.2

Choose an import form according to how explicitly you want names identified:

  • `import modulename` keeps calls qualified, making their origin clear and avoiding clashes with identically named objects in other modules. section 10.3
  • `from modulename import name1, name2` makes only those names directly available. section 10.3
  • `from modulename import *` imports public names without a prefix. It can overwrite an existing name and obscure where names came from; underscore-prefixed names are normally excluded, while `__all__` can specify what is imported. section 10.3

Python must be able to find the file before it can import the module. Keep a program-specific module beside the program, or put shared modules in a directory included in the module search path, visible through `sys.path`. In Colaboratory, upload the file to the current notebook session or create it there before importing it. section 10.4.1 section 10.1

Editing a module file does not make an existing interactive session use the changes automatically. After editing an imported module, use `importlib.reload(mymath)` to load the revised version. section 10.2

A file can serve as both a module and a runnable script. Put script-only work behind `if __name__ == '__main__':` so importing the file makes its functions available without running its main routine. section 11.1.3

When one module grows too broad, split related responsibilities into modules within a package. A package groups modules in a directory; for example, text-cleaning functions and word-counting functions can live in separate modules while remaining part of one package. section 18.1 section 18.6.2

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Works through file creation, import forms, name access, search paths, and reload without detailing the import system's implementation.
Examples
Uses a circle-area module, qualified function calls, and a text-processing refactor into separate modules.
What it assumes
Assumes familiarity with Python functions and files while introducing module creation and imports through a small example.
What you can do after
Build it from scratch: place related definitions in a Python file, import them from another file, and choose qualified or selective access.
How it connects
Connects modules to namespaces, import statements, scripts, packages, and the standard library.

Also explained in

Compare how 35 books explain module →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.