Python .spec_from_file_location() Examples
The following are 14
code examples of .spec_from_file_location().
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: config_util.py From indy-plenum with Apache License 2.0 | 7 votes |
def getInstalledConfig(installDir, configFile): """ Reads config from the installation directory of Plenum. :param installDir: installation directory of Plenum :param configFile: name of the configuration file :raises: FileNotFoundError :return: the configuration as a python object """ configPath = os.path.join(installDir, configFile) if not os.path.exists(configPath): raise FileNotFoundError("No file found at location {}". format(configPath)) spec = spec_from_file_location(configFile, configPath) config = module_from_spec(spec) spec.loader.exec_module(config) return config
Example #2
Source File: base.py From eNMS with GNU General Public License v3.0 | 6 votes |
def init_services(self): path_services = [self.path / "eNMS" / "services"] if self.settings["paths"]["custom_services"]: path_services.append(Path(self.settings["paths"]["custom_services"])) for path in path_services: for file in path.glob("**/*.py"): if "init" in str(file): continue if not self.settings["app"]["create_examples"] and "examples" in str( file ): continue info(f"Loading service: {file}") spec = spec_from_file_location(str(file).split("/")[-1][:-3], str(file)) try: spec.loader.exec_module(module_from_spec(spec)) except InvalidRequestError as exc: error(f"Error loading custom service '{file}' ({str(exc)})")
Example #3
Source File: controller.py From kytos with MIT License | 6 votes |
def _import_napp(self, username, napp_name): """Import a NApp module. Raises: FileNotFoundError: if NApp's main.py is not found. ModuleNotFoundError: if any NApp requirement is not installed. """ mod_name = '.'.join(['napps', username, napp_name, 'main']) path = os.path.join(self.options.napps, username, napp_name, 'main.py') napp_spec = spec_from_file_location(mod_name, path) napp_module = module_from_spec(napp_spec) sys.modules[napp_spec.name] = napp_module napp_spec.loader.exec_module(napp_module) return napp_module
Example #4
Source File: scripts.py From catalyst with Apache License 2.0 | 6 votes |
def import_module(expdir: pathlib.Path): """ @TODO: Docs. Contribution is welcome """ # @TODO: better PYTHONPATH handling if not isinstance(expdir, pathlib.Path): expdir = pathlib.Path(expdir) sys.path.insert(0, str(expdir.absolute())) sys.path.insert(0, os.path.dirname(str(expdir.absolute()))) s = spec_from_file_location( expdir.name, str(expdir.absolute() / "__init__.py"), submodule_search_locations=[expdir.absolute()], ) m = module_from_spec(s) s.loader.exec_module(m) sys.modules[expdir.name] = m return m
Example #5
Source File: setup.py From quantumsim with GNU General Public License v3.0 | 5 votes |
def get_version_and_cmdclass(package_path): import os from importlib.util import module_from_spec, spec_from_file_location spec = spec_from_file_location('version', os.path.join(package_path, '_version.py')) module = module_from_spec(spec) spec.loader.exec_module(module) return module.__version__, module.cmdclass
Example #6
Source File: setup.py From qmt with MIT License | 5 votes |
def get_version_and_cmdclass(package_name): import os from importlib.util import module_from_spec, spec_from_file_location spec = spec_from_file_location("version", os.path.join(package_name, "_version.py")) module = module_from_spec(spec) spec.loader.exec_module(module) return module.__version__, module.cmdclass
Example #7
Source File: 1_2_188_to_1_2_189.py From indy-node with Apache License 2.0 | 5 votes |
def get_network_name(): network_name = 'sandbox' old_general_config = os.path.join(old_base_dir, 'indy_config.py') spec = spec_from_file_location('old_general_config', old_general_config) old_cfg = module_from_spec(spec) spec.loader.exec_module(old_cfg) if hasattr(old_cfg, 'poolTransactionsFile'): network_name = _get_network_from_txn_file_name(old_cfg.poolTransactionsFile) elif hasattr(old_cfg, 'domainTransactionsFile'): network_name = _get_network_from_txn_file_name(old_cfg.domainTransactionsFile) elif hasattr(old_cfg, 'current_env') and old_cfg.current_env != 'test': network_name = old_cfg.current_env return network_name
Example #8
Source File: base.py From eNMS with GNU General Public License v3.0 | 5 votes |
def init_forms(self): for file in (self.path / "eNMS" / "forms").glob("**/*.py"): spec = spec_from_file_location(str(file).split("/")[-1][:-3], str(file)) spec.loader.exec_module(module_from_spec(spec))
Example #9
Source File: config_util.py From old-sovrin with Apache License 2.0 | 5 votes |
def getInstalledConfig(installDir, configFile): configPath = os.path.join(installDir, configFile) if os.path.exists(configPath): spec = spec_from_file_location(configFile, configPath) config = module_from_spec(spec) spec.loader.exec_module(config) return config else: raise FileNotFoundError("No file found at location {}". format(configPath))
Example #10
Source File: qconfig.py From qiskit-ibmq-provider with Apache License 2.0 | 5 votes |
def read_credentials_from_qconfig() -> Dict[HubGroupProject, Credentials]: """Read the ``QConfig.py`` file and return its credentials. Returns: A dictionary with the read credentials, in the ``{credentials_unique_id: Credentials}`` format. Raises: InvalidCredentialsFormatError: If the ``Qconfig.py`` file could not be parsed. Note that this exception is not raised if the input file does not exist, and an empty dictionary is returned instead. """ if not os.path.isfile(DEFAULT_QCONFIG_FILE): return OrderedDict() else: # Note this is nested inside the else to prevent some tools marking # the whole method as deprecated. pass # TODO: reintroduce when we decide on deprecating # warnings.warn( # "Using 'Qconfig.py' for storing the credentials will be deprecated in" # "upcoming versions (>0.6.0). Using .qiskitrc is recommended", # DeprecationWarning) try: spec = spec_from_file_location('Qconfig', DEFAULT_QCONFIG_FILE) q_config = module_from_spec(spec) spec.loader.exec_module(q_config) # type: ignore[attr-defined] if hasattr(q_config, 'config'): credentials = q_config.config.copy() # type: ignore[attr-defined] else: credentials = {} credentials['token'] = q_config.APItoken # type: ignore[attr-defined] credentials['url'] = credentials.get('url', QE_URL) except Exception as ex: # pylint: disable=broad-except raise InvalidCredentialsFormatError( 'Error loading Qconfig.py: {}'.format(str(ex))) from ex credentials = Credentials(**credentials) return OrderedDict({credentials.unique_id(): credentials})
Example #11
Source File: util.py From pretf with MIT License | 5 votes |
def import_file(path: Union[PurePath, str]) -> Generator[ModuleType, None, None]: """ Imports a Python module from any local filesystem path. Temporarily alters sys.path to allow the imported module to import other modules in the same directory. """ pathdir = os.path.dirname(path) if pathdir in sys.path: added_to_sys_path = False else: sys.path.insert(0, pathdir) added_to_sys_path = True try: name = os.path.basename(path).split(".")[0] spec = spec_from_file_location(name, str(path)) module = module_from_spec(spec) assert isinstance(spec.loader, Loader) loader: Loader = spec.loader try: loader.exec_module(module) except Exception as error: log.bad(error) raise yield module finally: if added_to_sys_path: sys.path.remove(pathdir)
Example #12
Source File: setup.py From adaptive with BSD 3-Clause "New" or "Revised" License | 5 votes |
def get_version_and_cmdclass(package_name): import os from importlib.util import module_from_spec, spec_from_file_location spec = spec_from_file_location("version", os.path.join(package_name, "_version.py")) module = module_from_spec(spec) spec.loader.exec_module(module) return module.__version__, module.cmdclass
Example #13
Source File: plugin_helper.py From indy-plenum with Apache License 2.0 | 4 votes |
def loadPlugins(plugins_dir, plugins_to_load=None): global pluginsLoaded alreadyLoadedPlugins = pluginsLoaded.get(plugins_dir) i = 0 if alreadyLoadedPlugins: logger.debug("Plugins {} are already loaded from plugins_dir: {}".format( alreadyLoadedPlugins, plugins_dir)) else: logger.debug( "Plugin loading started to load plugins from plugins_dir: {}".format( plugins_dir)) if not os.path.exists(plugins_dir): os.makedirs(plugins_dir) logger.debug("Plugin directory created at: {}".format( plugins_dir)) if plugins_to_load is not None: for pluginName in plugins_to_load: pluginPath = os.path.expanduser( os.path.join(plugins_dir, pluginName + ".py")) try: if os.path.exists(pluginPath): spec = spec_from_file_location( pluginName, pluginPath) plugin = module_from_spec(spec) spec.loader.exec_module(plugin) if plugins_dir in pluginsLoaded: pluginsLoaded[plugins_dir].add(pluginName) else: pluginsLoaded[plugins_dir] = {pluginName} i += 1 else: if not pluginsNotFound.get(pluginPath): logger.error("Note: Plugin file does not exists: {}. " "Create plugin file if you want to load it" .format(pluginPath), extra={"cli": False}) pluginsNotFound[pluginPath] = "Notified" except Exception as ex: # TODO: Is this strategy ok to catch any exception and # just print the error and continue, # or it should fail if there is error in plugin loading logger.error("** Error occurred during loading plugin {}: {}".format(pluginPath, str(ex))) logger.debug( "Total plugins loaded from plugins_dir {} are : {}".format(plugins_dir, i)) return i
Example #14
Source File: setup.py From cyvlfeat with BSD 2-Clause "Simplified" License | 4 votes |
def get_version_and_cmdclass(package_name): import os from importlib.util import module_from_spec, spec_from_file_location spec = spec_from_file_location('version', os.path.join(package_name, '_version.py')) module = module_from_spec(spec) spec.loader.exec_module(module) return module.__version__, module.cmdclass