Python Glossary Русский

Python 3.14+inspectfunction

inspect.ispackage()

members and modules

Checks that an object is a package, a module with a __path__ attribute that submodules are imported from. Added in 3.14.

Syntax

inspect.ispackage(object) -> bool

A package is a module but not the other way round: ismodule(json) and ispackage(json) are both True, while for os the second is False even though os.path makes it look like a package. A submodule such as json.decoder is a plain module. A string is never treated as a package: pass the imported object. Before 3.14 the same check is hasattr(mod, '__path__').

Examples

import inspect, json, json.decoder, os
print(inspect.ispackage(json), inspect.ispackage(os))  # → True False
print(inspect.ispackage(json.decoder), inspect.ispackage('json'))  # → False False
print(hasattr(json, '__path__'))  # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy