import inspect import importlib class_name = "FirstChildClass" file_path = "/path/to/class.py" try: spec = importlib.util.spec_from_file_location(class_name, file_path) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) for x in dir(module): obj = getattr(module, x) if inspect.isclass(obj) and (issubclass(obj, FirstParentClass) or issubclass(obj, SecondParentClass)) and obj.__name__ == class_name: return obj() except ImportError as e: print(e)
Такой способ работает, но нужно знать имя загружаемого класса. А можно ли как-то обойтись без имени класса, ведь класс в файле всего один?