user2558053 user2558053 - 7 months ago 100
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.


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'>