本文整理汇总了Python中importlib.util方法的典型用法代码示例。如果您正苦于以下问题:Python importlib.util方法的具体用法?Python importlib.util怎么用?Python importlib.util使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类importlib
的用法示例。
在下文中一共展示了importlib.util方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: import_module_by_path
# 需要导入模块: import importlib [as 别名]
# 或者: from importlib import util [as 别名]
def import_module_by_path(path):
name = os.path.splitext(os.path.basename(path))[0]
if sys.version_info[0] == 2:
import imp
return imp.load_source(name, path)
elif sys.version_info[:2] <= (3, 4):
from importlib.machinery import SourceFileLoader
return SourceFileLoader(name, path).load_module()
else:
import importlib.util
spec = importlib.util.spec_from_file_location(name, path)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod
# ===================================================================
# --- others
# ===================================================================
示例2: load_module
# 需要导入模块: import importlib [as 别名]
# 或者: from importlib import util [as 别名]
def load_module(module_path):
if "/" not in module_path:
import importlib
return importlib.import_module("%s" % module_path, package="armory")
else:
module_name = module_path.split("/")[-1]
if sys.version_info.major == 2:
import imp
return imp.load_source(module_name, module_path + ".py")
else:
import importlib.util
spec = importlib.util.spec_from_file_location(
module_name, module_path + ".py"
)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
示例3: load_dashboard
# 需要导入模块: import importlib [as 别名]
# 或者: from importlib import util [as 别名]
def load_dashboard(path):
"""Load a ``Dashboard`` from a Python definition.
:param str path: Path to a *.dashboard.py file that defines a variable,
``dashboard``.
:return: A ``Dashboard``
"""
if sys.version_info[0] == 3 and sys.version_info[1] >= 5:
import importlib.util
spec = importlib.util.spec_from_file_location("dashboard", path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
else:
import importlib
module = importlib.load_source("dashboard", path)
marker = object()
dashboard = getattr(module, 'dashboard', marker)
if dashboard is marker:
raise DashboardError(
"Dashboard definition {} does not define 'dashboard'".format(path))
return dashboard
示例4: check_fortran_library
# 需要导入模块: import importlib [as 别名]
# 或者: from importlib import util [as 别名]
def check_fortran_library(path):
if not path:
return None
def _check_library(module):
spec = importlib.util.spec_from_file_location(module, path)
try:
importlib.util.module_from_spec(spec)
except ImportError:
return False
else:
return True
if _check_library('pyOM_code'):
return 'sequential'
if _check_library('pyOM_code_MPI'):
return 'parallel'
return None
示例5: find_loader
# 需要导入模块: import importlib [as 别名]
# 或者: from importlib import util [as 别名]
def find_loader(fullname):
"""Find a PEP 302 "loader" object for fullname
This is a backwards compatibility wrapper around
importlib.util.find_spec that converts most failures to ImportError
and only returns the loader rather than the full spec
"""
if fullname.startswith('.'):
msg = "Relative module name {!r} not supported".format(fullname)
raise ImportError(msg)
try:
spec = importlib.util.find_spec(fullname)
except (ImportError, AttributeError, TypeError, ValueError) as ex:
# This hack fixes an impedance mismatch between pkgutil and
# importlib, where the latter raises other errors for cases where
# pkgutil previously raised ImportError
msg = "Error while finding loader for {!r} ({}: {})"
raise ImportError(msg.format(fullname, type(ex), ex)) from ex
return spec.loader if spec is not None else None
示例6: test_timestamp_overflow
# 需要导入模块: import importlib [as 别名]
# 或者: from importlib import util [as 别名]
def test_timestamp_overflow(self):
# A modification timestamp larger than 2**32 should not be a problem
# when importing a module (issue #11235).
sys.path.insert(0, os.curdir)
try:
source = TESTFN + ".py"
compiled = importlib.util.cache_from_source(source)
with open(source, 'w') as f:
pass
try:
os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5))
except OverflowError:
self.skipTest("cannot set modification time to large integer")
except OSError as e:
if e.errno not in (getattr(errno, 'EOVERFLOW', None),
getattr(errno, 'EINVAL', None)):
raise
self.skipTest("cannot set modification time to large integer ({})".format(e))
__import__(TESTFN)
# The pyc file was created.
os.stat(compiled)
finally:
del sys.path[0]
remove_files(TESTFN)
示例7: test_package___cached__
# 需要导入模块: import importlib [as 别名]
# 或者: from importlib import util [as 别名]
def test_package___cached__(self):
# Like test___cached__ but for packages.
def cleanup():
rmtree('pep3147')
unload('pep3147.foo')
unload('pep3147')
os.mkdir('pep3147')
self.addCleanup(cleanup)
# Touch the __init__.py
with open(os.path.join('pep3147', '__init__.py'), 'w'):
pass
with open(os.path.join('pep3147', 'foo.py'), 'w'):
pass
importlib.invalidate_caches()
m = __import__('pep3147.foo')
init_pyc = importlib.util.cache_from_source(
os.path.join('pep3147', '__init__.py'))
self.assertEqual(m.__cached__, os.path.join(os.curdir, init_pyc))
foo_pyc = importlib.util.cache_from_source(os.path.join('pep3147', 'foo.py'))
self.assertEqual(sys.modules['pep3147.foo'].__cached__,
os.path.join(os.curdir, foo_pyc))
示例8: test_issue24748_load_module_skips_sys_modules_check
# 需要导入模块: import importlib [as 别名]
# 或者: from importlib import util [as 别名]
def test_issue24748_load_module_skips_sys_modules_check(self):
name = 'test.imp_dummy'
try:
del sys.modules[name]
except KeyError:
pass
try:
module = importlib.import_module(name)
spec = importlib.util.find_spec('_testmultiphase')
module = imp.load_dynamic(name, spec.origin)
self.assertEqual(module.__name__, name)
self.assertEqual(module.__spec__.name, name)
self.assertEqual(module.__spec__.origin, spec.origin)
self.assertRaises(AttributeError, getattr, module, 'dummy_name')
self.assertEqual(module.int_const, 1969)
self.assertIs(sys.modules[name], module)
finally:
try:
del sys.modules[name]
except KeyError:
pass
示例9: _check_path_limitations
# 需要导入模块: import importlib [as 别名]
# 或者: from importlib import util [as 别名]
def _check_path_limitations(self, module_name):
# base directory
source_path_len = len(self.here)
# a path separator + `longname` (twice)
source_path_len += 2 * (len(self.longname) + 1)
# a path separator + `module_name` + ".py"
source_path_len += len(module_name) + 1 + len(".py")
cached_path_len = (source_path_len +
len(importlib.util.cache_from_source("x.py")) - len("x.py"))
if os.name == 'nt' and cached_path_len >= 258:
# Under Windows, the max path len is 260 including C's terminating
# NUL character.
# (see http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx#maxpath)
self.skipTest("test paths too long (%d characters) for Windows' 260 character limit"
% cached_path_len)
elif os.name == 'nt' and verbose:
print("cached_path_len =", cached_path_len)
示例10: test_functionality
# 需要导入模块: import importlib [as 别名]
# 或者: from importlib import util [as 别名]
def test_functionality(self):
'''Test basic functionality of stuff defined in an extension module'''
with util.uncache(self.name):
module = self.load_module()
self.assertIsInstance(module, types.ModuleType)
ex = module.Example()
self.assertEqual(ex.demo('abcd'), 'abcd')
self.assertEqual(ex.demo(), None)
with self.assertRaises(AttributeError):
ex.abc
ex.abc = 0
self.assertEqual(ex.abc, 0)
self.assertEqual(module.foo(9, 9), 18)
self.assertIsInstance(module.Str(), str)
self.assertEqual(module.Str(1) + '23', '123')
with self.assertRaises(module.error):
raise module.error()
self.assertEqual(module.int_const, 1969)
self.assertEqual(module.str_const, 'something different')
示例11: find_loader
# 需要导入模块: import importlib [as 别名]
# 或者: from importlib import util [as 别名]
def find_loader(fullname):
"""Find a "loader" object for fullname
This is a backwards compatibility wrapper around
importlib.util.find_spec that converts most failures to ImportError
and only returns the loader rather than the full spec
"""
if fullname.startswith('.'):
msg = "Relative module name {!r} not supported".format(fullname)
raise ImportError(msg)
try:
spec = importlib.util.find_spec(fullname)
except (ImportError, AttributeError, TypeError, ValueError) as ex:
# This hack fixes an impedance mismatch between pkgutil and
# importlib, where the latter raises other errors for cases where
# pkgutil previously raised ImportError
msg = "Error while finding loader for {!r} ({}: {})"
raise ImportError(msg.format(fullname, type(ex), ex)) from ex
return spec.loader if spec is not None else None
示例12: get_kwargs
# 需要导入模块: import importlib [as 别名]
# 或者: from importlib import util [as 别名]
def get_kwargs(setupfilepath: str):
setupfilepath = Path(setupfilepath)
setup_kwargs = {}
def grab_setup_kwargs(**kwargs):
setup_kwargs.update(kwargs)
with chdir(str(setupfilepath.parent)):
spec = importlib.util.spec_from_file_location(
"setup", str(setupfilepath.parts[-1])
)
with unittest.mock.patch(
"setuptools.setup", new=grab_setup_kwargs
):
setup = importlib.util.module_from_spec(spec)
spec.loader.exec_module(setup)
return setup_kwargs
示例13: _importlib_import_modpath
# 需要导入模块: import importlib [as 别名]
# 或者: from importlib import util [as 别名]
def _importlib_import_modpath(modpath): # nocover
"""
Alternative to import_module_from_path using importlib mechainsms
"""
dpath, rel_modpath = split_modpath(modpath)
modname = modpath_to_modname(modpath)
if six.PY2: # nocover
import imp
module = imp.load_source(modname, modpath)
elif sys.version_info[0:2] <= (3, 4): # nocover
if sys.version_info[0:2] <= (3, 2):
raise AssertionError('3.0 to 3.2 is not supported')
from importlib.machinery import SourceFileLoader
module = SourceFileLoader(modname, modpath).load_module()
else:
import importlib.util
spec = importlib.util.spec_from_file_location(modname, modpath)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
示例14: test_timestamp_overflow
# 需要导入模块: import importlib [as 别名]
# 或者: from importlib import util [as 别名]
def test_timestamp_overflow(self):
# A modification timestamp larger than 2**32 should not be a problem
# when importing a module (issue #11235).
sys.path.insert(0, os.curdir)
try:
source = TESTFN + ".py"
compiled = importlib.util.cache_from_source(source)
with open(source, 'w') as f:
pass
try:
os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5))
except OverflowError:
self.skipTest("cannot set modification time to large integer")
except OSError as e:
if e.errno != getattr(errno, 'EOVERFLOW', None):
raise
self.skipTest("cannot set modification time to large integer ({})".format(e))
__import__(TESTFN)
# The pyc file was created.
os.stat(compiled)
finally:
del sys.path[0]
remove_files(TESTFN)
示例15: forget
# 需要导入模块: import importlib [as 别名]
# 或者: from importlib import util [as 别名]
def forget(modname):
"""'Forget' a module was ever imported.
This removes the module from sys.modules and deletes any PEP 3147 or
legacy .pyc and .pyo files.
"""
unload(modname)
for dirname in sys.path:
source = os.path.join(dirname, modname + '.py')
# It doesn't matter if they exist or not, unlink all possible
# combinations of PEP 3147 and legacy pyc and pyo files.
unlink(source + 'c')
unlink(source + 'o')
unlink(importlib.util.cache_from_source(source, debug_override=True))
unlink(importlib.util.cache_from_source(source, debug_override=False))
# Check whether a gui is actually available