Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 996
user2558053 Avatar asked Aug 24 '15 06:08

user2558053


People also ask

What is the use of imp module in Python?

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).

What is the use of import module in Python?

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.

What is the use of importlib in Python?

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.

What are the submodules of importlib?

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.


1 Answers

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'>
like image 58
Anand S Kumar Avatar answered Oct 16 '22 08:10

Anand S Kumar