當前位置: 首頁>>代碼示例>>Python>>正文


Python importlib.find_loader方法代碼示例

本文整理匯總了Python中importlib.find_loader方法的典型用法代碼示例。如果您正苦於以下問題:Python importlib.find_loader方法的具體用法?Python importlib.find_loader怎麽用?Python importlib.find_loader使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在importlib的用法示例。


在下文中一共展示了importlib.find_loader方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: find_module_py33

# 需要導入模塊: import importlib [as 別名]
# 或者: from importlib import find_loader [as 別名]
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 
開發者ID:ktraunmueller,項目名稱:Computable,代碼行數:32,代碼來源:_compatibility.py

示例2: check_if_installed

# 需要導入模塊: import importlib [as 別名]
# 或者: from importlib import find_loader [as 別名]
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 
開發者ID:SlapBot,項目名稱:stephanie-va,代碼行數:8,代碼來源:install.py

示例3: find_module_py33

# 需要導入模塊: import importlib [as 別名]
# 或者: from importlib import find_loader [as 別名]
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 
開發者ID:MichaelAquilina,項目名稱:python-tools,代碼行數:37,代碼來源:_compatibility.py

示例4: can_import

# 需要導入模塊: import importlib [as 別名]
# 或者: from importlib import find_loader [as 別名]
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 
開發者ID:deepgram,項目名稱:kur,代碼行數:28,代碼來源:importhelp.py

示例5: has_module

# 需要導入模塊: import importlib [as 別名]
# 或者: from importlib import find_loader [as 別名]
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 
開發者ID:domlysz,項目名稱:BlenderGIS,代碼行數:15,代碼來源:util.py

示例6: find_module

# 需要導入模塊: import importlib [as 別名]
# 或者: from importlib import find_loader [as 別名]
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. 
開發者ID:danielecook,項目名稱:gist-alfred,代碼行數:59,代碼來源:importer.py

示例7: find_module

# 需要導入模塊: import importlib [as 別名]
# 或者: from importlib import find_loader [as 別名]
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. 
開發者ID:AtomLinter,項目名稱:linter-pylama,代碼行數:57,代碼來源:importer.py

示例8: find_module

# 需要導入模塊: import importlib [as 別名]
# 或者: from importlib import find_loader [as 別名]
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 
開發者ID:operatorequals,項目名稱:httpimport,代碼行數:44,代碼來源:httpimport.py

示例9: find_module_py33

# 需要導入模塊: import importlib [as 別名]
# 或者: from importlib import find_loader [as 別名]
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 
開發者ID:autocomplete-python,項目名稱:autocomplete-python,代碼行數:47,代碼來源:_compatibility.py


注:本文中的importlib.find_loader方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。