Python Glossary Русский

all Python 3typesterm

types.ModuleType

interpreter objects

The type of a module: what import creates. types.ModuleType('name') builds a module by hand, without a file.

Syntax

types.ModuleType(name, doc=None)

isinstance(x, types.ModuleType) is a reliable way to tell a module from a class or a dict. A module built with the constructor is empty and registered nowhere: import will not find it until you put it into sys.modules yourself. It is a trick for tests and stubs, not a way to load a file — importlib does that.

Examples

import math
import sys
import types
print(isinstance(math, types.ModuleType))  # → True
fake = types.ModuleType('settings')
fake.DEBUG = True
sys.modules['settings'] = fake
import settings
print(settings.DEBUG)  # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy