PyCharm shows me that imp is deprecated so I wonder if there any analogue of imp.new_module for importlib.
Deprecated since version 3.4: The imp module is deprecated in favor of importlib. This module provides an interface to the mechanisms used to implement the import statement. It defines the following constants and functions: Return the magic string value used to recognize byte-compiled code files ( .pyc files).
This package also exposes components to implement import, making it easier for users to create their own custom objects (known as an importer) to participate in the import process. The importlib package has an important function named as import_module () This function imports a module programmatically.
The purpose of the importlib package is two-fold. One is to provide the implementation of the import statement (and thus, by extension, the __import__() function) in Python source code. This provides an implementation of import which is portable to any Python interpreter.
The importlib package contains following submodules: This module contains all of the core abstract base classes used by import. Some subclasses of the core abstract base classes are also provided to help in implementing the core ABCs This module leverages Python’s import system to provide access to resources within packages.
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'>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With