I have:
from pathlib import Path
if some_condition:
path1 = Path(r'path/to/file.py')
else:
path1 = Path(r'path/to/other_file.py')
And I want to dynamically import class class1
from that path (both file.py
and other_file.py
have a class named class1
). How can I do that?
UPDATE:
I managed to get the module object successfully:
import importlib.util
file_name = path1.name
spec = importlib.util.spec_from_file_location(file_name, path1)
module = importlib.util.module_from_spec(spec)
But I still don't know how to get class1
from the module object.