Roméo Després
Roméo Després

Reputation: 2163

How to import the `module` class?

I want to work on the module class in Python, but I don't know how to find it. I know I can get it as follows:

>>> import os
>>> type(os)  # This is what I want
<class 'module'>

but I was wondering if there is any cleaner way to import it. The following does not work:

>>> type(os).__module__
'builtins'
>>> from builtins import module  # OK so let's import it
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'module' from 'builtins' (unknown location)

Upvotes: 2

Views: 115

Answers (1)

U13-Forward
U13-Forward

Reputation: 71570

Try using types.ModuleType:

>>> import types
>>> types.ModuleType
<class 'module'>
>>> 

[types] is a module which helps you to deal with types, as in the docs:

This module defines utility functions to assist in dynamic creation of new types.

There it tells you:

Finally, it provides some additional type-related utility classes and functions that are not fundamental enough to be builtins.

That is why it is not in the builtins.

Upvotes: 3

Related Questions