Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of imp.find_module in importlib

Some of my code use the now deprecated imp package to find a module

toolboxFile, toolboxPath, toolboxDescription = imp.find_module("Tools") 

What is the equivalent function or code to get the same results with package importlib ?

like image 754
Marc Avatar asked Feb 09 '16 09:02

Marc


People also ask

What is Importlib Import_module?

The import_module() function acts as a simplifying wrapper around importlib. __import__() . This means all semantics of the function are derived from importlib. __import__() . The most important difference between these two functions is that import_module() returns the specified package or module (e.g. pkg.

What is the imp function in Python?

The imp module exposes the implementation of Python's import statement. Available In: At least 2.2.1. The imp module includes functions that expose part of the underlying implementation of Python's import mechanism for loading code in packages and modules.

What is __ import __ in Python?

__import__() Parameters name - the name of the module you want to import. globals and locals - determines how to interpret name. fromlist - objects or submodules that should be imported by name. level - specifies whether to use absolute or relative imports.

How do I import a Python path?

append() Function. This is the easiest way to import a Python module by adding the module path to the path variable. The path variable contains the directories Python interpreter looks in for finding modules that were imported in the source files.


1 Answers

Same folder

If the module is in your working directory, importlib.util.find_spec probably suffices for your purposes.

For example if you just want to load the module, you can use:

  • deprecated in Python 3.5 and higher:

    toolbox_specs = importlib.util.find_spec("Tools") toolbox = toolbox_specs.loader.load_module() 
  • introduced in Python 3.5 and higher:

    toolbox_specs = importlib.util.find_spec("Tools") toolbox = importlib.util.module_from_spec(toolbox_specs) toolbox_specs.loader.exec_module(toolbox) 

    Caveat: I haven’t tested this, but it’s straight from the documentation, so I suppose it works.

You can assess several other properties with the toolbox_specs object. However, e.g., a corresponding file object is not amongst them. If you really need this in Python 3, you probably have to obtain the file’s path and open it with other methods.

Different folder

To find a module in a different folder, you have to work with a FileFinder, which in turn needs to know the module’s type. For example, if your module is an extension, you can find the specs as follows:

loader_details = (     importlib.machinery.ExtensionFileLoader,     importlib.machinery.EXTENSION_SUFFIXES     )  toolsfinder = importlib.machinery.FileFinder("Folder_of_Tools", loader_details) toolbox_specs = toolsfinder.find_spec("Tools") 

You can then process toolbox_specs as described above.

like image 100
Wrzlprmft Avatar answered Sep 22 '22 11:09

Wrzlprmft