Python importlib.util() Examples
The following are 30
code examples of importlib.util().
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: easter_eggs.py From zdict with GNU General Public License v3.0 | 7 votes |
def import_pyjokes_module(): v = os.environ.get('VIRTUAL_ENV', None) if v: sys.path = [v + '/lib/python3.5/site-packages'] + sys.path if importlib.util.find_spec('pyjokes'): return importlib.import_module('pyjokes')
Example #2
Source File: __init__.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
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)
Example #3
Source File: __init__.py From vnpy_crypto with MIT License | 6 votes |
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 # ===================================================================
Example #4
Source File: __init__.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
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))
Example #5
Source File: test_reprlib.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
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)
Example #6
Source File: __init__.py From ironpython3 with Apache License 2.0 | 6 votes |
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
Example #7
Source File: test_reprlib.py From ironpython3 with Apache License 2.0 | 6 votes |
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)
Example #8
Source File: test_runpy.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_run_name(self): depth = 1 run_name = "And now for something completely different" pkg_dir, mod_fname, mod_name, mod_spec = ( self._make_pkg(example_source, depth)) forget(mod_name) expected_ns = example_namespace.copy() expected_ns.update({ "__name__": run_name, "__file__": mod_fname, "__cached__": importlib.util.cache_from_source(mod_fname), "__package__": mod_name.rpartition(".")[0], "__spec__": mod_spec, }) def create_ns(init_globals): return run_module(mod_name, init_globals, run_name) try: self.check_code_execution(create_ns, expected_ns) finally: self._del_pkg(pkg_dir, depth, mod_name)
Example #9
Source File: test_imp.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
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
Example #10
Source File: armory.py From armory with GNU General Public License v3.0 | 6 votes |
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
Example #11
Source File: _gen.py From grafanalib with Apache License 2.0 | 6 votes |
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
Example #12
Source File: pkgutil.py From ironpython3 with Apache License 2.0 | 6 votes |
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
Example #13
Source File: __init__.py From psutil with BSD 3-Clause "New" or "Revised" License | 6 votes |
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 # ===================================================================
Example #14
Source File: pkgutil.py From Imogen with MIT License | 6 votes |
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
Example #15
Source File: pkgutil.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
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
Example #16
Source File: dev.py From dffml with MIT License | 6 votes |
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
Example #17
Source File: run_benchmarks.py From veros with MIT License | 6 votes |
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
Example #18
Source File: util_import.py From ubelt with Apache License 2.0 | 6 votes |
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
Example #19
Source File: test_loader.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
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')
Example #20
Source File: test_import.py From ironpython3 with Apache License 2.0 | 6 votes |
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)
Example #21
Source File: test_loader.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_is_package(self): self.assertFalse(self.loader.is_package(util.EXTENSIONS.name)) for suffix in self.machinery.EXTENSION_SUFFIXES: path = os.path.join('some', 'path', 'pkg', '__init__' + suffix) loader = self.machinery.ExtensionFileLoader('pkg', path) self.assertTrue(loader.is_package('pkg'))
Example #22
Source File: config.py From quart with MIT License | 5 votes |
def from_pyfile(self, filename: str, silent: bool = False) -> None: """Load the configuration from a Python cfg or py file. See Python's ConfigParser docs for details on the cfg format. It is a common practice to load the defaults from the source using the :meth:`from_object` and then override with a cfg or py file, for example .. code-block:: python app.config.from_object('config_module') app.config.from_pyfile('production.cfg') Arguments: filename: The filename which when appended to :attr:`root_path` gives the path to the file """ file_path = self.root_path / filename try: spec = importlib.util.spec_from_file_location("module.name", file_path) if spec is None: # Likely passed a cfg file parser = ConfigParser() parser.optionxform = str # type: ignore # Prevents lowercasing of keys with open(file_path) as file_: config_str = "[section]\n" + file_.read() parser.read_string(config_str) self.from_mapping(parser["section"]) else: module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) # type: ignore self.from_object(module) except (FileNotFoundError, IsADirectoryError): if not silent: raise
Example #23
Source File: test_loader.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_equality(self): other = self.machinery.ExtensionFileLoader(util.EXTENSIONS.name, util.EXTENSIONS.file_path) self.assertEqual(self.loader, other)
Example #24
Source File: pkgutil.py From Imogen with MIT License | 5 votes |
def _get_spec(finder, name): """Return the finder-specific module spec.""" # Works with legacy finders. try: find_spec = finder.find_spec except AttributeError: loader = finder.find_module(name) if loader is None: return None return importlib.util.spec_from_loader(name, loader) else: return find_spec(name)
Example #25
Source File: test_loader.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def setUp(self): self.loader = self.machinery.ExtensionFileLoader(util.EXTENSIONS.name, util.EXTENSIONS.file_path)
Example #26
Source File: test_loader.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_reload(self): '''Test that reload didn't re-set the module's attributes''' with util.uncache(self.name): module = self.load_module() ex_class = module.Example importlib.reload(module) self.assertIs(ex_class, module.Example)
Example #27
Source File: test_loader.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_inequality(self): other = self.machinery.ExtensionFileLoader('_' + util.EXTENSIONS.name, util.EXTENSIONS.file_path) self.assertNotEqual(self.loader, other)
Example #28
Source File: test_loader.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_module(self): with util.uncache(util.EXTENSIONS.name): module = self.load_module(util.EXTENSIONS.name) for attr, value in [('__name__', util.EXTENSIONS.name), ('__file__', util.EXTENSIONS.file_path), ('__package__', '')]: self.assertEqual(getattr(module, attr), value) self.assertIn(util.EXTENSIONS.name, sys.modules) self.assertIsInstance(module.__loader__, self.machinery.ExtensionFileLoader) # No extension module as __init__ available for testing.
Example #29
Source File: test_loader.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_module(self): '''Test loading an extension module''' with util.uncache(self.name): module = self.load_module() for attr, value in [('__name__', self.name), ('__file__', self.spec.origin), ('__package__', '')]: self.assertEqual(getattr(module, attr), value) with self.assertRaises(AttributeError): module.__path__ self.assertIs(module, sys.modules[self.name]) self.assertIsInstance(module.__loader__, self.machinery.ExtensionFileLoader)
Example #30
Source File: test_loader.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def setUp(self): self.name = '_testmultiphase' finder = self.machinery.FileFinder(None) self.spec = importlib.util.find_spec(self.name) assert self.spec self.loader = self.machinery.ExtensionFileLoader( self.name, self.spec.origin) # No extension module as __init__ available for testing.