Reputation: 10063
Is there a way in Python to get the module from a type ? I have an object which I can get the type but I am unable to see where the type is defined.
Upvotes: 5
Views: 1107
Reputation: 387647
You can use inspect.getmodule
to retrieve the module object the specified object was defined in.
For example:
>>> import inspect
>>> from collections import Counter
>>> c = Counter()
>>> inspect.getmodule(c)
<module 'collections' from 'C:\\Program Files\\Python34\\lib\\collections\\__init__.py'>
>>> inspect.getmodule(Counter)
<module 'collections' from 'C:\\Program Files\\Python34\\lib\\collections\\__init__.py'>
Upvotes: 4