user2558053 user2558053 - 1 year ago 276
Python Question

Python importlib's analogue for imp.new_module()

PyCharm shows me that imp is deprecated so I wonder if there any analogue of imp.new_module for importlib.

Answer Source

Quoting from documentation (Emphasis mine) -


Return a new empty module object called name. This object is not inserted in sys.modules.

Deprecated since version 3.4: Use types.ModuleType instead.

Example -

>>> import types
>>> types.ModuleType('name')
<module 'name'>

To show how they are synonymous -

>>> import imp
>>> imp.new_module('name')
<module 'name'>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download