all Python 3inspectfunction
inspect.getmodule()
members and modules
Tries to work out the module an object was defined in; returns the module object or None.
Syntax
inspect.getmodule(object) -> ModuleType | None
The answer is the defining module, not the one you imported from: getmodule(os.path.join) gives posixpath on Linux and macOS and ntpath on Windows. For a number, a string or a function built with exec() you get None, so check the result before reading .__name__. Do not confuse it with getmodulename(), which takes a file path and returns a string.
Examples
import inspect, os
def f():
pass
print(inspect.getmodule(f).__name__) # → __main__
print(inspect.getmodule(len).__name__) # → builtins
print(inspect.getmodule(42)) # → None
print(inspect.getmodule(os.path.join).__name__) # → ? posixpath или ntpath
See also
Open in the full glossarydocs.python.orgReport an inaccuracy