Python Language Importing modules The __all__ special variable


Modules can have a special variable named __all__ to restrict what variables are imported when using from mymodule import *.

Given the following module:


__all__ = ['imported_by_star']

imported_by_star = 42
not_imported_by_star = 21

Only imported_by_star is imported when using from mymodule import *:

>>> from mymodule import *
>>> imported_by_star
>>> not_imported_by_star
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'not_imported_by_star' is not defined

However, not_imported_by_star can be imported explicitly:

>>> from mymodule import not_imported_by_star
>>> not_imported_by_star