user2558053 user2558053 - 4 months ago 46
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

Quoting from documentation (Emphasis mine) -

imp.new_module(name)

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