What is the equivalent of imp.find_module in importlib

imp.find_module example
imp.find_module importlib
python importlib from path
importlib resources
python dynamic import by filename
importlib pyd
pymotw 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 ?

imp — Access the import internals, find_module() . If you called imp.load_module() and related functions directly with file path arguments then use a combination of importlib.util. importlib.resources.open_text (package, resource, encoding='utf-8', errors='strict') ¶ Open for text reading the resource within package. By default, the resource is opened for reading as UTF-8. package is either a name or a module object which conforms to the Package requirements.

I found this worked as a replacement for imp.find_module()

importlib.machinery.PathFinder().find_spec(modName, modDirList)

importlib — The implementation of import, find_module() which is equivalent to self.find_loader(fullname)[0] . Deprecated since version 3.4: Use find_spec() instead. imp.find_module() does not find modules from zipped eggs. How can find modules which can come from both places: directories or zipped eggs? It is important in my case that I can provide a path argument like imp.find_module() supports it. Background. Somehow packages get installed twice in our environment. As zipped egg and as plain files.

According to Python official documentation in this page imp

find_module Deprecated since version 3.3 Use importlib.util.find_spec() instead unless Python 3.3 compatibility is required, in which case use importlib.find_loader().

Use importlib.util.find_spec("Tools") for find spec for more information you can see this link.

And use importlib.find_loader("Tools") for find loader more information


sample code

import importlib
import importlib.util
import sys

# this is optional set that if you what load from specific directory
        spec = importlib.util.find_spec(name,moduledir)
        if spec is None:
            print("Import error 0: " + " module not found")
        toolbox = spec.loader.load_module()
    except (ValueError, ImportError) as msg:
        print("Import error 3: "+str(msg))

    print("load module")

Importlib, imp, find_module, find_spec issue · Issue #972 · python , This includes imp module deprecation (replaced by importlib), This includes imp module deprecation (replaced by importlib ), "wrong" usage of find_spec and find_module I haven't considered the /dyn alternative so far. imp.load_module (name, file, pathname, description) ¶ Load a module that was previously found by find_module() (or by an otherwise conducted search yielding compatible results). This function does more than importing the module: if the module was already imported, it is equivalent to a reload() !

Replace uses of imp.find_module / imp.load_module with importlib , Replace uses of imp.find_module / imp.load_module with importlib in the documentation for alternative uses from imp import find_module,  31.5.1. Introduction¶. 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.

importlib — Python's Import Mechanism, The importlib module includes functions that implement Python's import Then to retrieve the module, use the loader's load_module() method. Python 3.5+: How to dynamically import a module given the full file path (in the presence of implicit sibling imports)? Ask Question Asked 3 years, 2 months ago

imp, _bootstrap_external import SourcelessFileLoader from importlib import in favour of importlib; " "see the module's documentation for alternative uses", directory', path=path) def find_module(self, fullname): """Always returns None.""" return  I am personally not bothering with moving imp.get_tag() and imp.get_magic() to importlib._bootstrap as the amount of C code required to support the public C API is not worth it. If someone else once to do the work then by all means attach a patch. I still need to port imp.find_module() (and the various constants) and will base it off of Eric's

  • If Tools does an implicit sibling import (e.g. import foo and there is a foo.py file along side Tools.py), this solution yields ModuleNotFoundError: No module named 'foo'. Any way to address this?
  • @PedroCattori: I am not entirely sure what your situation is, let allone how to address it. I suggest you ask a separate question on this.
  • Can you address whether and how any of these commands allows you to find modules in a different path than the working path?
  • Your example code does not work for me if moduledir is not the current directory (also the indentation is broken), even if there is a module in that directory that would be found otherwise. Does it work for you? If yes, we may have found a bug. If no, we either have a bug or misunderstood how it works.
  • Yes it works for me version test with python version 3.4.4
  • As it stands, this code does not work and is not intended to do so. find_spec does not take a directory as second argument. The variant with find_loader should not have worked either, as this takes a list of directiories and not a directory as an argument.