Python .module_from_spec() Examples

The following are 7 code examples of .module_from_spec(). 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 , or try the search function .
Example #1
Source File: renderer.py    From mkdocs-pdf-export-plugin with MIT License 6 votes vote down vote up
def _load_theme_handler(theme: str, custom_handler_path: str = None):
        module_name = '.' + (theme or 'generic').replace('-', '_')

        if custom_handler_path:
            try:
                spec = spec_from_file_location(module_name, os.path.join(os.getcwd(), custom_handler_path))
                mod = module_from_spec(spec)
                spec.loader.exec_module(mod)
                return mod
            except FileNotFoundError as e:
                print('Could not load theme handler {} from custom directory "{}": {}'.format(theme, custom_handler_path, e), file=sys.stderr)
                pass

        try:
            return import_module(module_name, 'mkdocs_pdf_export_plugin.themes')
        except ImportError as e:
            print('Could not load theme handler {}: {}'.format(theme, e), file=sys.stderr)
            return generic_theme 
Example #2
Source File: object.py    From mayo with MIT License 6 votes vote down vote up
def import_from_file(path):
    """
    Import module from path
    """
    name = os.path.split(path)[1]
    name = os.path.splitext(name)[0]
    spec = spec_from_file_location(name, path)
    if spec is None:
        raise ImportError(
            'Unable to find module {!r} in path {!r}.'.format(name, path))
    module = module_from_spec(spec)
    spec.loader.exec_module(module)
    return module 
Example #3
Source File: jit.py    From pytorch_geometric with MIT License 5 votes vote down vote up
def class_from_module_repr(cls_name, module_repr):
    path = osp.join('/tmp', 'pyg_jit')
    makedirs(path)
    with TempFile(mode='w+', suffix='.py', delete=False, dir=path) as f:
        f.write(module_repr)
    spec = spec_from_file_location(cls_name, f.name)
    mod = module_from_spec(spec)
    sys.modules[cls_name] = mod
    spec.loader.exec_module(mod)
    return getattr(mod, cls_name) 
Example #4
Source File: __init__.py    From WaveRNN with MIT License 5 votes vote down vote up
def _import_from_file(name, path: Path):
    """Programmatically returns a module object from a filepath"""
    if not Path(path).exists():
        raise FileNotFoundError('"%s" doesn\'t exist!' % path)
    spec = spec_from_file_location(name, path)
    if spec is None:
        raise ValueError('could not load module from "%s"' % path)
    m = module_from_spec(spec)
    spec.loader.exec_module(m)
    return m 
Example #5
Source File: render.py    From flaskerize with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _load_run_function(self, path: str) -> Callable:
        from importlib.util import spec_from_file_location, module_from_spec

        spec = spec_from_file_location("run", path)

        module = module_from_spec(spec)
        spec.loader.exec_module(module)
        if not hasattr(module, "run"):
            raise ValueError(f"No method 'run' function found in {path}")
        return getattr(module, "run") 
Example #6
Source File: render.py    From flaskerize with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _load_custom_functions(self, path: str) -> None:
        import os

        from flaskerize import registered_funcs
        from importlib.util import spec_from_file_location, module_from_spec

        if not os.path.exists(path):
            return
        spec = spec_from_file_location("custom_functions", path)

        module = module_from_spec(spec)
        spec.loader.exec_module(module)

        for f in registered_funcs:
            self.env.globals[f.__name__] = f 
Example #7
Source File: setup.py    From operator with Apache License 2.0 5 votes vote down vote up
def _get_version() -> str:
    """Get the version via ops/version.py, without loading ops/__init__.py"""
    spec = spec_from_file_location('ops.version', 'ops/version.py')
    module = module_from_spec(spec)
    spec.loader.exec_module(module)

    return module.version