Python importlib.find_loader() Examples
The following are 16
code examples of importlib.find_loader().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
importlib
, or try the search function
.
Example #1
Source File: _compatibility.py From Computable with MIT License | 5 votes |
def find_module_py33(string, path=None): loader = importlib.machinery.PathFinder.find_module(string, path) if loader is None and path is None: # Fallback to find builtins loader = importlib.find_loader(string) if loader is None: raise ImportError("Couldn't find a loader for {0}".format(string)) try: is_package = loader.is_package(string) if is_package: module_path = os.path.dirname(loader.path) module_file = None else: module_path = loader.get_filename(string) module_file = open(module_path, 'rb') except AttributeError: # ExtensionLoader has not attribute get_filename, instead it has a # path attribute that we can use to retrieve the module path try: module_path = loader.path module_file = open(loader.path, 'rb') except AttributeError: module_path = string module_file = None finally: is_package = False return module_file, module_path, is_package
Example #2
Source File: _compatibility.py From NukeToolSet with MIT License | 5 votes |
def find_module_py33(string, path=None): loader = importlib.machinery.PathFinder.find_module(string, path) if loader is None and path is None: # Fallback to find builtins loader = importlib.find_loader(string) if loader is None: raise ImportError("Couldn't find a loader for {0}".format(string)) try: is_package = loader.is_package(string) if is_package: module_path = os.path.dirname(loader.path) module_file = None else: module_path = loader.get_filename(string) module_file = open(module_path, 'rb') except AttributeError: # ExtensionLoader has not attribute get_filename, instead it has a # path attribute that we can use to retrieve the module path try: module_path = loader.path module_file = open(loader.path, 'rb') except AttributeError: module_path = string module_file = None finally: is_package = False return module_file, module_path, is_package
Example #3
Source File: install.py From stephanie-va with MIT License | 5 votes |
def check_if_installed(module_name): try: module_status = importlib.find_loader(module_name) except Exception: module_status = importlib.util.find_spec(module_name) return module_status
Example #4
Source File: _compatibility.py From python-tools with MIT License | 5 votes |
def find_module_py33(string, path=None): loader = importlib.machinery.PathFinder.find_module(string, path) if loader is None and path is None: # Fallback to find builtins try: loader = importlib.find_loader(string) except ValueError as e: # See #491. Importlib might raise a ValueError, to avoid this, we # just raise an ImportError to fix the issue. raise ImportError("Originally ValueError: " + e.message) if loader is None: raise ImportError("Couldn't find a loader for {0}".format(string)) try: is_package = loader.is_package(string) if is_package: module_path = os.path.dirname(loader.path) module_file = None else: module_path = loader.get_filename(string) module_file = open(module_path, 'rb') except AttributeError: # ExtensionLoader has not attribute get_filename, instead it has a # path attribute that we can use to retrieve the module path try: module_path = loader.path module_file = open(loader.path, 'rb') except AttributeError: module_path = string module_file = None finally: is_package = False return module_file, module_path, is_package
Example #5
Source File: importhelp.py From kur with Apache License 2.0 | 5 votes |
def can_import(name, package=None): """ Returns True if the given module can be imported. # Arguments name: str. The name of the module. package: str. The name of the package, if `name` is a relative import. This is ignored for Python versions < 3.4. # Return value If importing the specified module should succeed, returns True; otherwise, returns False. """ try: importlib.util.find_spec except AttributeError: # Python < 3.4 return importlib.find_loader( # pylint: disable=deprecated-method name ) is not None else: # Python >= 3.4 return importlib.util.find_spec(name, package=package) is not None ### EOF.EOF.EOF.EOF.EOF.EOF.EOF.EOF.EOF.EOF.EOF.EOF.EOF.EOF.EOF.EOF.EOF.EOF.EOF
Example #6
Source File: _compatibility.py From CNCGToolKit with MIT License | 5 votes |
def find_module_py33(string, path=None): loader = importlib.machinery.PathFinder.find_module(string, path) if loader is None and path is None: # Fallback to find builtins loader = importlib.find_loader(string) if loader is None: raise ImportError("Couldn't find a loader for {0}".format(string)) try: is_package = loader.is_package(string) if is_package: module_path = os.path.dirname(loader.path) module_file = None else: module_path = loader.get_filename(string) module_file = open(module_path, 'rb') except AttributeError: # ExtensionLoader has not attribute get_filename, instead it has a # path attribute that we can use to retrieve the module path try: module_path = loader.path module_file = open(loader.path, 'rb') except AttributeError: module_path = string module_file = None finally: is_package = False return module_file, module_path, is_package
Example #7
Source File: util.py From BlenderGIS with GNU General Public License v3.0 | 5 votes |
def has_module(module_name): """Check to see if a python module is available. """ if sys.version_info > (3, ): import importlib return importlib.find_loader(module_name) is not None else: # pragma: no cover import imp try: imp.find_module(module_name) except ImportError: return False return True
Example #8
Source File: importer.py From gist-alfred with MIT License | 4 votes |
def find_module(self, fullname, path=None): # If the module being imported is not one we have registered # post import hooks for, we can return immediately. We will # take no further part in the importing of this module. if not fullname in _post_import_hooks: return None # When we are interested in a specific module, we will call back # into the import system a second time to defer to the import # finder that is supposed to handle the importing of the module. # We set an in progress flag for the target module so that on # the second time through we don't trigger another call back # into the import system and cause a infinite loop. if fullname in self.in_progress: return None self.in_progress[fullname] = True # Now call back into the import system again. try: if PY3: # For Python 3 we need to use find_spec().loader # from the importlib.util module. It doesn't actually # import the target module and only finds the # loader. If a loader is found, we need to return # our own loader which will then in turn call the # real loader to import the module and invoke the # post import hooks. try: import importlib.util loader = importlib.util.find_spec(fullname).loader except (ImportError, AttributeError): loader = importlib.find_loader(fullname, path) if loader: return _ImportHookChainedLoader(loader) else: # For Python 2 we don't have much choice but to # call back in to __import__(). This will # actually cause the module to be imported. If no # module could be found then ImportError will be # raised. Otherwise we return a loader which # returns the already loaded module and invokes # the post import hooks. __import__(fullname) return _ImportHookLoader() finally: del self.in_progress[fullname] # Decorator for marking that a function should be called as a post # import hook when the target module is imported.
Example #9
Source File: importer.py From linter-pylama with MIT License | 4 votes |
def find_module(self, fullname, path=None): # If the module being imported is not one we have registered # post import hooks for, we can return immediately. We will # take no further part in the importing of this module. if not fullname in _post_import_hooks: return None # When we are interested in a specific module, we will call back # into the import system a second time to defer to the import # finder that is supposed to handle the importing of the module. # We set an in progress flag for the target module so that on # the second time through we don't trigger another call back # into the import system and cause a infinite loop. if fullname in self.in_progress: return None self.in_progress[fullname] = True # Now call back into the import system again. try: if PY3: # For Python 3 we need to use find_loader() from # the importlib module. It doesn't actually # import the target module and only finds the # loader. If a loader is found, we need to return # our own loader which will then in turn call the # real loader to import the module and invoke the # post import hooks. loader = importlib.find_loader(fullname, path) if loader: return _ImportHookChainedLoader(loader) else: # For Python 2 we don't have much choice but to # call back in to __import__(). This will # actually cause the module to be imported. If no # module could be found then ImportError will be # raised. Otherwise we return a loader which # returns the already loaded module and invokes # the post import hooks. __import__(fullname) return _ImportHookLoader() finally: del self.in_progress[fullname] # Decorator for marking that a function should be called as a post # import hook when the target module is imported.
Example #10
Source File: importer.py From python-netsurv with MIT License | 4 votes |
def find_module(self, fullname, path=None): # If the module being imported is not one we have registered # post import hooks for, we can return immediately. We will # take no further part in the importing of this module. if not fullname in _post_import_hooks: return None # When we are interested in a specific module, we will call back # into the import system a second time to defer to the import # finder that is supposed to handle the importing of the module. # We set an in progress flag for the target module so that on # the second time through we don't trigger another call back # into the import system and cause a infinite loop. if fullname in self.in_progress: return None self.in_progress[fullname] = True # Now call back into the import system again. try: if PY3: # For Python 3 we need to use find_spec().loader # from the importlib.util module. It doesn't actually # import the target module and only finds the # loader. If a loader is found, we need to return # our own loader which will then in turn call the # real loader to import the module and invoke the # post import hooks. try: import importlib.util loader = importlib.util.find_spec(fullname).loader except (ImportError, AttributeError): loader = importlib.find_loader(fullname, path) if loader: return _ImportHookChainedLoader(loader) else: # For Python 2 we don't have much choice but to # call back in to __import__(). This will # actually cause the module to be imported. If no # module could be found then ImportError will be # raised. Otherwise we return a loader which # returns the already loaded module and invokes # the post import hooks. __import__(fullname) return _ImportHookLoader() finally: del self.in_progress[fullname] # Decorator for marking that a function should be called as a post # import hook when the target module is imported.
Example #11
Source File: importer.py From python-netsurv with MIT License | 4 votes |
def find_module(self, fullname, path=None): # If the module being imported is not one we have registered # post import hooks for, we can return immediately. We will # take no further part in the importing of this module. if not fullname in _post_import_hooks: return None # When we are interested in a specific module, we will call back # into the import system a second time to defer to the import # finder that is supposed to handle the importing of the module. # We set an in progress flag for the target module so that on # the second time through we don't trigger another call back # into the import system and cause a infinite loop. if fullname in self.in_progress: return None self.in_progress[fullname] = True # Now call back into the import system again. try: if PY3: # For Python 3 we need to use find_spec().loader # from the importlib.util module. It doesn't actually # import the target module and only finds the # loader. If a loader is found, we need to return # our own loader which will then in turn call the # real loader to import the module and invoke the # post import hooks. try: import importlib.util loader = importlib.util.find_spec(fullname).loader except (ImportError, AttributeError): loader = importlib.find_loader(fullname, path) if loader: return _ImportHookChainedLoader(loader) else: # For Python 2 we don't have much choice but to # call back in to __import__(). This will # actually cause the module to be imported. If no # module could be found then ImportError will be # raised. Otherwise we return a loader which # returns the already loaded module and invokes # the post import hooks. __import__(fullname) return _ImportHookLoader() finally: del self.in_progress[fullname] # Decorator for marking that a function should be called as a post # import hook when the target module is imported.
Example #12
Source File: httpimport.py From httpimport with Apache License 2.0 | 4 votes |
def find_module(self, fullname, path=None): logger.debug("FINDER=================") logger.debug("[!] Searching %s" % fullname) logger.debug("[!] Path is %s" % path) logger.info("[@] Checking if in declared remote module names >") if fullname.split('.')[0] not in self.module_names: logger.info("[-] Not found!") return None if fullname in self.in_progress: return None self.in_progress[fullname] = True logger.info("[@] Checking if built-in >") try: if LEGACY: loader = imp.find_module(fullname, path) else: try: # After Python3.5 loader = importlib.util.find_spec(fullname, path) except AttributeError: loader = importlib.find_loader(fullname, path) if loader: logger.info("[-] Found locally!") return None except ImportError: pass logger.info("[@] Checking if it is name repetition >") if fullname.split('.').count(fullname.split('.')[-1]) > 1: logger.info("[-] Found locally!") return None if self.is_archive: logger.info("[@] Checking if module exists in loaded Archive file >") if self._mod_in_archive(fullname) is None: logger.info("[-] Not Found in Archive file!") return None logger.info("[*] Module/Package '%s' can be loaded!" % fullname) del(self.in_progress[fullname]) return self
Example #13
Source File: importer.py From apm-agent-python with BSD 3-Clause "New" or "Revised" License | 4 votes |
def find_module(self, fullname, path=None): # If the module being imported is not one we have registered # post import hooks for, we can return immediately. We will # take no further part in the importing of this module. if not fullname in _post_import_hooks: return None # When we are interested in a specific module, we will call back # into the import system a second time to defer to the import # finder that is supposed to handle the importing of the module. # We set an in progress flag for the target module so that on # the second time through we don't trigger another call back # into the import system and cause a infinite loop. if fullname in self.in_progress: return None self.in_progress[fullname] = True # Now call back into the import system again. try: if PY3: # For Python 3 we need to use find_loader() from # the importlib module. It doesn't actually # import the target module and only finds the # loader. If a loader is found, we need to return # our own loader which will then in turn call the # real loader to import the module and invoke the # post import hooks. loader = importlib.find_loader(fullname, path) if loader: return _ImportHookChainedLoader(loader) else: # For Python 2 we don't have much choice but to # call back in to __import__(). This will # actually cause the module to be imported. If no # module could be found then ImportError will be # raised. Otherwise we return a loader which # returns the already loaded module and invokes # the post import hooks. __import__(fullname) return _ImportHookLoader() finally: del self.in_progress[fullname] # Decorator for marking that a function should be called as a post # import hook when the target module is imported.
Example #14
Source File: _compatibility.py From autocomplete-python with GNU General Public License v2.0 | 4 votes |
def find_module_py33(string, path=None): loader = importlib.machinery.PathFinder.find_module(string, path) if loader is None and path is None: # Fallback to find builtins try: loader = importlib.find_loader(string) except ValueError as e: # See #491. Importlib might raise a ValueError, to avoid this, we # just raise an ImportError to fix the issue. raise ImportError("Originally " + repr(e)) if loader is None: raise ImportError("Couldn't find a loader for {0}".format(string)) try: is_package = loader.is_package(string) if is_package: if hasattr(loader, 'path'): module_path = os.path.dirname(loader.path) else: # At least zipimporter does not have path attribute module_path = os.path.dirname(loader.get_filename(string)) if hasattr(loader, 'archive'): module_file = DummyFile(loader, string) else: module_file = None else: module_path = loader.get_filename(string) module_file = DummyFile(loader, string) except AttributeError: # ExtensionLoader has not attribute get_filename, instead it has a # path attribute that we can use to retrieve the module path try: module_path = loader.path module_file = DummyFile(loader, string) except AttributeError: module_path = string module_file = None finally: is_package = False if hasattr(loader, 'archive'): module_path = loader.archive return module_file, module_path, is_package
Example #15
Source File: importer.py From lambda-text-extractor with Apache License 2.0 | 4 votes |
def find_module(self, fullname, path=None): # If the module being imported is not one we have registered # post import hooks for, we can return immediately. We will # take no further part in the importing of this module. if not fullname in _post_import_hooks: return None # When we are interested in a specific module, we will call back # into the import system a second time to defer to the import # finder that is supposed to handle the importing of the module. # We set an in progress flag for the target module so that on # the second time through we don't trigger another call back # into the import system and cause a infinite loop. if fullname in self.in_progress: return None self.in_progress[fullname] = True # Now call back into the import system again. try: if PY3: # For Python 3 we need to use find_loader() from # the importlib module. It doesn't actually # import the target module and only finds the # loader. If a loader is found, we need to return # our own loader which will then in turn call the # real loader to import the module and invoke the # post import hooks. loader = importlib.find_loader(fullname, path) if loader: return _ImportHookChainedLoader(loader) else: # For Python 2 we don't have much choice but to # call back in to __import__(). This will # actually cause the module to be imported. If no # module could be found then ImportError will be # raised. Otherwise we return a loader which # returns the already loaded module and invokes # the post import hooks. __import__(fullname) return _ImportHookLoader() finally: del self.in_progress[fullname] # Decorator for marking that a function should be called as a post # import hook when the target module is imported.
Example #16
Source File: importer.py From lambda-text-extractor with Apache License 2.0 | 4 votes |
def find_module(self, fullname, path=None): # If the module being imported is not one we have registered # post import hooks for, we can return immediately. We will # take no further part in the importing of this module. if not fullname in _post_import_hooks: return None # When we are interested in a specific module, we will call back # into the import system a second time to defer to the import # finder that is supposed to handle the importing of the module. # We set an in progress flag for the target module so that on # the second time through we don't trigger another call back # into the import system and cause a infinite loop. if fullname in self.in_progress: return None self.in_progress[fullname] = True # Now call back into the import system again. try: if PY3: # For Python 3 we need to use find_loader() from # the importlib module. It doesn't actually # import the target module and only finds the # loader. If a loader is found, we need to return # our own loader which will then in turn call the # real loader to import the module and invoke the # post import hooks. loader = importlib.find_loader(fullname, path) if loader: return _ImportHookChainedLoader(loader) else: # For Python 2 we don't have much choice but to # call back in to __import__(). This will # actually cause the module to be imported. If no # module could be found then ImportError will be # raised. Otherwise we return a loader which # returns the already loaded module and invokes # the post import hooks. __import__(fullname) return _ImportHookLoader() finally: del self.in_progress[fullname] # Decorator for marking that a function should be called as a post # import hook when the target module is imported.