Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Farrukhraz/f0c28a55e101947e17109ef39657ffa6 to your computer and use it in GitHub Desktop.
Save Farrukhraz/f0c28a55e101947e17109ef39657ffa6 to your computer and use it in GitHub Desktop.
Dynamic imports
import importlib
import sys
robot_folder_path = r'full\path\to\folder\with\packages'
module_name = 'PackageName'
file_path = f'{robot_folder_path}\\{module_name}\\__init__.py'
spec = importlib.util.spec_from_file_location(module_name, file_path)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)
init_class = module.Class_name()
import importlib
import sys
def load_module_class(module_name, class_name, robot_folder_path):
"""
module_name: 'xmlschemes.scenario' or 'PythonEngine'
class_name: 'Resources' or 'Engine'
"""
module_name = module_name.split(".")
module = module_loader(robot_folder_path, module_name[0])
module_class = class_loader(module, module_name, class_name)
return module_class
def module_loader(folder_path, module_name):
"""
folder_path = 'path\to\folder\with\packages'
module_name = 'PythonEngine'
"""
file_path = f'{folder_path}\\{module_name}\\__init__.py'
spec = importlib.util.spec_from_file_location(module_name, file_path)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)
return module
def class_loader(module, module_name, class_name):
if not isinstance(module_name, list):
module_name = module_name.split(".")
assert len(module_name) > 0
if len(module_name) == 1:
module_class = getattr(module, class_name)
return module_class
if len(module_name) == 2:
module = getattr(module, module_name[1])
module_class = getattr(module, class_name)
return module_class
else:
module = getattr(module, module_name[1])
class_loader(module, module_name[1:], class_name)
import sys
import types
import importlib.abc
import importlib.machinery
import importlib.util
class MockImporter(importlib.abc.MetaPathFinder, importlib.abc.Loader):
def __init__(self, modules):
self.modules = modules
def find_spec(self, fullname, path, target=None):
print('find_spec', fullname, path)
contents = self.modules.get(fullname)
if contents is None:
return None
return importlib.machinery.ModuleSpec(
fullname, self)
def create_module(self, spec):
module = types.ModuleType(spec.name)
module.__dict__.update(self.modules[spec.name])
return module
def exec_module(self, module):
pass
def main():
modules = {
'foo': {
'attr': 'value',
},
'foo.bar': {
'attr', 'value',
}
}
sys.meta_path.append(MockImporter(modules))
from foo import bar
if __name__ == '__main__':
main()
import importlib # Can't import modules which aren't in current project.
def dynamic_import(module):
return importlib.import_module(module)
module = self.dynamic_import(path + "." + "foo") # shold look like this: package.subpackage.subsubpackage.foo
module.main()
import importlib
module = importlib.import_module('app.plugins')
plugin_class = getattr(module, 'PluginName')
plugin_instance = plugin_class(*args, **kwargs) # Like standard instance = Class(params)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment