5

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.

1

1 Answer 1

4

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'>
Sign up to request clarification or add additional context in comments.

2 Comments

On this particular example, I am getting <module 'builtin' (built-in)>
Then it’s part of the builtin module which is built into the interpreter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.