本文整理汇总了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
示例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
示例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
示例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
示例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
示例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.
示例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.
示例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
示例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