本文整理汇总了Python中pkgutil.get_loader方法的典型用法代码示例。如果您正苦于以下问题:Python pkgutil.get_loader方法的具体用法?Python pkgutil.get_loader怎么用?Python pkgutil.get_loader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pkgutil
的用法示例。
在下文中一共展示了pkgutil.get_loader方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: find_package
# 需要导入模块: import pkgutil [as 别名]
# 或者: from pkgutil import get_loader [as 别名]
def find_package(name: str) -> Tuple[Optional[Path], Path]:
"""Finds packages install prefix (or None) and it's containing Folder
"""
module = name.split(".")[0]
loader = pkgutil.get_loader(module)
if name == "__main__" or loader is None:
package_path = Path.cwd()
else:
if hasattr(loader, "get_filename"):
filename = loader.get_filename(module) # type: ignore
else:
__import__(name)
filename = sys.modules[name].__file__
package_path = Path(filename).resolve().parent
if hasattr(loader, "is_package"):
is_package = loader.is_package(module) # type: ignore
if is_package:
package_path = Path(package_path).resolve().parent
sys_prefix = Path(sys.prefix).resolve()
try:
package_path.relative_to(sys_prefix)
except ValueError:
return None, package_path
else:
return sys_prefix, package_path
示例2: _get_module_details
# 需要导入模块: import pkgutil [as 别名]
# 或者: from pkgutil import get_loader [as 别名]
def _get_module_details(mod_name, error=ImportError):
try:
loader = get_loader(mod_name)
if loader is None:
raise error("No module named %s" % mod_name)
ispkg = loader.is_package(mod_name)
except ImportError as e:
raise error(format(e))
if ispkg:
if mod_name == "__main__" or mod_name.endswith(".__main__"):
raise error("Cannot use package as __main__ module")
__import__(mod_name) # Do not catch exceptions initializing package
try:
pkg_main_name = mod_name + ".__main__"
return _get_module_details(pkg_main_name)
except ImportError, e:
raise error(("%s; %r is a package and cannot " +
"be directly executed") %(e, mod_name))
示例3: check_python_import
# 需要导入模块: import pkgutil [as 别名]
# 或者: from pkgutil import get_loader [as 别名]
def check_python_import(package_or_module):
"""
Checks if a python package or module is importable.
Arguments:
package_or_module -- the package or module name to check
Returns:
True or False
"""
logger = logging.getLogger(__name__)
logger.debug("Checking python import '%s'...", package_or_module)
loader = pkgutil.get_loader(package_or_module)
found = loader is not None
if found:
logger.debug("Python %s '%s' found: %r",
"package" if loader.is_package(package_or_module)
else "module", package_or_module, loader.get_filename())
else:
logger.debug("Python import '%s' not found", package_or_module)
return found
示例4: __init__
# 需要导入模块: import pkgutil [as 别名]
# 或者: from pkgutil import get_loader [as 别名]
def __init__(self, entrypoint):
self.entrypoint = entrypoint
# Compute path without loading the module
path = pkgutil.get_loader(entrypoint.module_name).path
path = os.path.dirname(path)
super(ConfigurableTheme, self).__init__(path)
self.variants = self.info.get('variants', [])
if 'default' not in self.variants:
self.variants.insert(0, 'default')
self.context_processors = {}
# Check JSON manifest
manifest = os.path.join(path, 'manifest.json')
if os.path.exists(manifest):
self.manifest = manifest
示例5: run_module
# 需要导入模块: import pkgutil [as 别名]
# 或者: from pkgutil import get_loader [as 别名]
def run_module(mod_name, init_globals=None,
run_name=None, alter_sys=False):
"""Execute a module's code without importing it
Returns the resulting top level namespace dictionary
"""
loader = get_loader(mod_name)
if loader is None:
raise ImportError("No module named " + mod_name)
code = loader.get_code(mod_name)
if code is None:
raise ImportError("No code object available for " + mod_name)
filename = _get_filename(loader, mod_name)
if run_name is None:
run_name = mod_name
return _run_module_code(code, init_globals, run_name,
filename, loader, alter_sys)
示例6: test_loader_get_code
# 需要导入模块: import pkgutil [as 别名]
# 或者: from pkgutil import get_loader [as 别名]
def test_loader_get_code(self):
# Execute Python code out of the JAR
jar = self.prepareJar('classimport.jar')
Thread.currentThread().contextClassLoader = test_support.make_jar_classloader(jar)
loader = pkgutil.get_loader('jar_pkg')
space = { 'value':None, 'compiled':None}
# flat_in_jar contains the assignment value = 7
code = loader.get_code('flat_in_jar')
exec code in space
self.assertEquals(space['value'], 7)
# jar_pkg.prefer_compiled contains the assignment compiled = False
code = loader.get_code('jar_pkg.prefer_compiled')
exec code in space
self.assertEquals(space['compiled'], False)
# Compile a new one containing the assignment compiled = True
self.compileToJar(jar)
code = loader.get_code('jar_pkg.prefer_compiled')
exec code in space
self.assertEquals(space['compiled'], True)
示例7: _find_root_path
# 需要导入模块: import pkgutil [as 别名]
# 或者: from pkgutil import get_loader [as 别名]
def _find_root_path(self, root_path: Optional[str] = None) -> Path:
if root_path is not None:
return Path(root_path)
else:
module = sys.modules.get(self.import_name)
if module is not None and hasattr(module, "__file__"):
file_path = module.__file__
else:
loader = pkgutil.get_loader(self.import_name)
if loader is None or self.import_name == "__main__":
return Path.cwd()
else:
file_path = loader.get_filename(self.import_name) # type: ignore
return Path(file_path).resolve().parent
示例8: _get_module_details
# 需要导入模块: import pkgutil [as 别名]
# 或者: from pkgutil import get_loader [as 别名]
def _get_module_details(mod_name):
loader = get_loader(mod_name)
if loader is None:
raise ImportError("No module named %s" % mod_name)
if loader.is_package(mod_name):
if mod_name == "__main__" or mod_name.endswith(".__main__"):
raise ImportError("Cannot use package as __main__ module")
try:
pkg_main_name = mod_name + ".__main__"
return _get_module_details(pkg_main_name)
except ImportError, e:
raise ImportError(("%s; %r is a package and cannot " +
"be directly executed") %(e, mod_name))
示例9: _manually_unlock_mount_encrypted_disk
# 需要导入模块: import pkgutil [as 别名]
# 或者: from pkgutil import get_loader [as 别名]
def _manually_unlock_mount_encrypted_disk(repair_group_name, repair_vm_name):
# Unlocks the disk using the phasephrase and mounts it on the repair VM.
REPAIR_DIR_NAME = 'azext_vm_repair'
SCRIPTS_DIR_NAME = 'scripts'
LINUX_RUN_SCRIPT_NAME = 'mount-encrypted-disk.sh'
command_id = 'RunShellScript'
loader = pkgutil.get_loader(REPAIR_DIR_NAME)
mod = loader.load_module(REPAIR_DIR_NAME)
rootpath = os.path.dirname(mod.__file__)
run_script = os.path.join(rootpath, SCRIPTS_DIR_NAME, LINUX_RUN_SCRIPT_NAME)
mount_disk_command = 'az vm run-command invoke -g {rg} -n {vm} --command-id {command_id} ' \
'--scripts "@{run_script}" -o json' \
.format(rg=repair_group_name, vm=repair_vm_name, command_id=command_id, run_script=run_script)
_call_az_command(mount_disk_command)
示例10: get_fullname
# 需要导入模块: import pkgutil [as 别名]
# 或者: from pkgutil import get_loader [as 别名]
def get_fullname(mod_name):
if IS_PY3K:
import pkgutil
else:
from _pydev_imps import _pydev_pkgutil_old as pkgutil
try:
loader = pkgutil.get_loader(mod_name)
except:
return None
if loader is not None:
for attr in ("get_filename", "_get_filename"):
meth = getattr(loader, attr, None)
if meth is not None:
return meth(mod_name)
return None
示例11: load_mnist
# 需要导入模块: import pkgutil [as 别名]
# 或者: from pkgutil import get_loader [as 别名]
def load_mnist(threshold=255/2):
"""
Get the MNIST data.
@param threshold: The position to threshold the data at. Values >= to the
threshold are set to 1, all other values are set to 0. Setting this to None
returns the raw data.
@return: A tuple of tuple containing: (train_x, train_y), (test_x, test_y).
"""
# Get the raw data
p = os.path.join(pkgutil.get_loader('mHTM.datasets').filename, 'mnist.pkl')
with open(p, 'rb') as f:
(tr_x, tr_y), (te_x, te_y) = cPickle.load(f)
# Threshold the data
if threshold is not None:
tr_x[tr_x < threshold] = 0
tr_x[tr_x > 0] = 1
tr_x = np.array(tr_x, dtype='bool')
te_x[te_x < threshold] = 0
te_x[te_x > 0] = 1
te_x = np.array(te_x, dtype='bool')
return (tr_x, tr_y), (te_x, te_y)
###############################################################################
# SP dataset
###############################################################################
示例12: test_get_loader_avoids_emulation
# 需要导入模块: import pkgutil [as 别名]
# 或者: from pkgutil import get_loader [as 别名]
def test_get_loader_avoids_emulation(self):
with check_warnings() as w:
self.assertIsNotNone(pkgutil.get_loader("sys"))
self.assertIsNotNone(pkgutil.get_loader("os"))
self.assertIsNotNone(pkgutil.get_loader("test.support"))
self.assertEqual(len(w.warnings), 0)
示例13: test_get_loader_handles_missing_loader_attribute
# 需要导入模块: import pkgutil [as 别名]
# 或者: from pkgutil import get_loader [as 别名]
def test_get_loader_handles_missing_loader_attribute(self):
global __loader__
this_loader = __loader__
del __loader__
try:
with check_warnings() as w:
self.assertIsNotNone(pkgutil.get_loader(__name__))
self.assertEqual(len(w.warnings), 0)
finally:
__loader__ = this_loader
示例14: test_get_loader_handles_missing_spec_attribute
# 需要导入模块: import pkgutil [as 别名]
# 或者: from pkgutil import get_loader [as 别名]
def test_get_loader_handles_missing_spec_attribute(self):
name = 'spam'
mod = type(sys)(name)
del mod.__spec__
with CleanImport(name):
sys.modules[name] = mod
loader = pkgutil.get_loader(name)
self.assertIsNone(loader)
示例15: test_get_loader_handles_spec_attribute_none
# 需要导入模块: import pkgutil [as 别名]
# 或者: from pkgutil import get_loader [as 别名]
def test_get_loader_handles_spec_attribute_none(self):
name = 'spam'
mod = type(sys)(name)
mod.__spec__ = None
with CleanImport(name):
sys.modules[name] = mod
loader = pkgutil.get_loader(name)
self.assertIsNone(loader)