liveBook · Manning

__all__

As explained in The Quick Python Book, Fourth Edition

__all__ is a module-level sequence that specifies the names exported by a wildcard import.

__all__ is a module- or package-level list of strings that selects the names made available by a wildcard import. In a package’s __init__.py, it defines that package’s wildcard-import surface. section 18.4 section 10.3

Use the following rules when reading or defining __all__:

  • Set it to the exact names that should be imported by from package_or_module import *. section 18.4
  • Do not expect wildcard imports to recurse into subpackages; import deeper names explicitly. section 18.4
  • Prefer leading underscores for elements that should remain nonpublic rather than using __all__ merely to hide them. section 18.7 section 18.5

Selecting package exports

With mathproj/__init__.py containing __all__ = ['comp'], from mathproj import * makes comp available. It does not make comp.c1 available; that requires from mathproj.comp import c1. section 18.3 section 18.4

from mathproj import *

Hello from mathproj init
Hello from mathproj.comp init

Shows the wildcard import, package initialization output, and the resulting exported comp name. section 18.4

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Defines export selection and demonstrates package behavior, but stops short of recursive importing and deeper package mechanics.
Examples
Demonstrates mathproj, comp, c1, and explicit imports through short interactive results.
What it assumes
Assumes familiarity with import statements and packages while clarifying wildcard-export selection.
What you can do after
Apply it via Python wildcard imports to select exported package names and avoid unintended names.
How it connects
Links wildcard imports with packages, __init__.py files, public names, and underscore naming.

Also explained in

Compare how 3 books explain __all__ →

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.