Programming Search⌘K

Featureadvanced

Module objects

Also called module object, types.ModuleType, module __dict__, __spec__, __loader__, __file__, module __getattr__, module __dir__, PEP 562, importlib.util.module_from_spec.

A module is an object with a namespace dictionary. The import system sets __name__, __spec__, __loader__, __file__ and __path__ from the spec, and a module can define __getattr__ and __dir__.

Language
Python

Synopsis

import shop.cart as m

m.x                       # the same as m.__dict__["x"]
m.x = 1                   # the same as m.__dict__["x"] = 1
m.__name__                # "shop.cart", or "__main__" for the program
m.__spec__                # the ModuleSpec: name, parent, origin, loader, submodule_search_locations
m.__file__, m.__loader__  # copies of spec fields; __path__ only on packages

# in a module: hooks for missing names and for dir()
def __getattr__(name): ...
def __dir__(): ...

Semantics

r1. "A module object has a namespace implemented by a dictionary object (this is the dictionary referenced by the globals attribute of functions defined in the module). Attribute references are translated to lookups in this dictionary, e.g., m.x is equivalent to m.dict["x"]."1 "Attribute assignment updates the module’s namespace dictionary".1 Example: namespace.

r2. __dict__ "cannot be accessed as a global variable from within a module; it can only be accessed as an attribute on module objects."1 Code in the module uses globals() for the same dictionary. Example: namespace.

r3. The import system sets the import-related attributes from the spec: "these attributes are filled in based on the module’s spec, before the loader executes and loads the module."1 The reference advises: "With the exception of name, it is strongly recommended that you rely on spec and its attributes instead of any of the other individual attributes listed in this subsection."1 Example: spec.

r4. __name__ is "The name used to uniquely identify the module in the import system."1 For a module that runs as the program, the value is '__main__'. __path__ is for packages only: "Non-package modules should not have a path attribute."1 __file__ "might be missing for certain types of modules, such as C modules that are statically linked into the interpreter".1 Example: spec.

r5. types.ModuleType is "The type of modules."2 "Modules created using the ModuleType constructor are created with many of their special attributes unset or set to default values."2 importlib.util.module_from_spec() "is preferred over using types.ModuleType to create a new module as spec is used to set as many import-controlled attributes on the module as possible."3 Example: create.

r6. A module can compute missing attributes. "If an attribute is not found on a module object through the normal lookup, i.e. object.getattribute(), then getattr is searched in the module dict before raising an AttributeError. If found, it is called with the attribute name and the result is returned."1 A module __dir__ "should accept no arguments, and return an iterable of strings that represents the names accessible on module."1 Example: getattr-hook.

r7. These hooks work only for attribute access: "Defining module getattr and setting module class only affect lookups made using the attribute access syntax – directly accessing the module globals (whether by code within the module, or via a reference to the module’s globals dictionary) is unaffected."1 Example: hook-not-global.

Examples

Module attributes are the entries of one dictionary (rules r1 and r2). A function of the module sees an assignment made from outside, because its __globals__ is that dictionary.

count = 0


def bump():
    global count
    count += 1
    return count


print("__dict__" in globals(), "count" in globals())
import counter

counter.bump()
print(counter.count, counter.__dict__["count"])

counter.count = 10
print(counter.bump())
print(counter.bump.__globals__ is counter.__dict__)
print(vars(counter) is counter.__dict__)
Output
False True
1 1
11
True
True

The spec holds the import state, and the module attributes repeat parts of it (rules r3 and r4). The built-in module sys has no file.

"""The shop package."""
ITEMS = []
import os
import sys

import shop.cart

cart = shop.cart
spec = cart.__spec__
print(cart.__name__, spec.name, spec.parent)
print(os.path.relpath(spec.origin), os.path.relpath(cart.__file__))
print(type(spec.loader).__name__, cart.__loader__ is spec.loader)
print(spec.submodule_search_locations)
print([os.path.relpath(p) for p in shop.__spec__.submodule_search_locations])
print(hasattr(cart, "__path__"), hasattr(shop, "__path__"))
print(hasattr(sys, "__file__"), sys.__spec__.origin)
Output
shop.cart shop.cart shop
shop/cart.py shop/cart.py
SourceFileLoader True
None
['shop']
False True
False built-in

A module made with types.ModuleType has no spec and no loader. module_from_spec sets them from a spec (rule r5).

def double(x):
    return 2 * x
import importlib.util
import os
import types

plain = types.ModuleType("plain", "A module made by hand.")
print(plain.__name__, plain.__doc__, plain.__spec__, plain.__loader__)
exec("def hello():\n    return 'hi from ' + __name__", plain.__dict__)
print(plain.hello())

spec = importlib.util.spec_from_file_location("tools", "tools.py")
tools = importlib.util.module_from_spec(spec)
spec.loader.exec_module(tools)
print(tools.__name__, os.path.relpath(tools.__file__), type(tools.__loader__).__name__)
print(tools.double(4))
Output
plain A module made by hand. None None
hi from plain
tools tools.py SourceFileLoader
8

A module __getattr__ keeps an old name working with a warning, and __dir__ hides it from dir() (rule r6). The old name is not in the module dictionary.

import warnings

METRE = 1.0
_RENAMED = {"meter": "METRE"}


def __getattr__(name):
    if name in _RENAMED:
        new = _RENAMED[name]
        warnings.warn(f"{name} is deprecated; use {new}", DeprecationWarning, stacklevel=2)
        return globals()[new]
    raise AttributeError(f"module {__name__!r} has no attribute {name!r}")


def __dir__():
    return ["METRE"]
import warnings

import units

with warnings.catch_warnings(record=True) as caught:
    warnings.simplefilter("always")
    print(units.meter)

print(caught[0].category.__name__, caught[0].message)
print(dir(units))
print("meter" in vars(units))
Output
1.0
DeprecationWarning meter is deprecated; use METRE
['METRE']
False

The examples give the same output on CPython 3.12.3, 3.13.14, 3.14.6 and 3.15.0b2.

Versions

The reference says of __cached__: "Changed in version 3.15: The cached attribute is no longer set on modules or taken into consideration by the import system or standard library."1 For shop.cart in spec, "__cached__" in vars(cart) is True on CPython 3.12.3, 3.13.14 and 3.14.6, and False on CPython 3.15.0b2. Use cart.__spec__.cached instead.

Pitfalls

The module __getattr__ is not used for globals

A module __getattr__ runs for module.name only (rule r7). Code inside the module that uses the old name as a global gets a NameError.

METRE = 1.0


def __getattr__(name):
    if name == "meter":
        return METRE
    raise AttributeError(name)


def length(n):
    return n * meter

This code raises NameError, on purpose.

import units

print(units.meter)
print(units.length(3))

Inside the module, use the real name.

A changed spec does not change the module

The attributes are copied from the spec when the module is made (rule r3). The reference warns: "Note that updating an attribute on spec will not update the corresponding attribute on the module itself".1

import json

json.__spec__.name = "other"
print(json.__name__, json.__spec__.name)

json.__name__ = "renamed"
print(json.__name__, json.__spec__.name)
Output
json other
renamed other

Do not change these attributes on a module that other code uses.

Comparison

JavaScript gives a module namespace object for import * as myModule. MDN says: "myModule is a sealed object with null prototype."4 Code cannot add a property to it. A Python module is an ordinary object, and m.x = 1 from any module changes its namespace (rule r1).

Implementation notes

"A module object does not contain the code object used to initialize the module (since it isn’t needed once the initialization is done)."1 The reference also notes a CPython detail of module dictionaries: "Because of the way CPython clears module dictionaries, the module dictionary will be cleared when the module falls out of scope even if the dictionary still has live references. To avoid this, copy the dictionary or keep the module around while using its dictionary directly."1

References

  1. The Python Language Reference, 3. Data model, 3.2.9. Modules; 3.2.9.1. Import-related attributes on module objects; 3.2.9.3. Module dictionaries; 3.3.2.1. Customizing module attribute access
  2. types — Dynamic type creation and names for built-in types, types.ModuleType
  3. importlib — The implementation of import, importlib.util.module_from_spec
  4. import, JavaScript reference, MDN, Namespace import

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source