I'm scratching my head to come up with a fast way to import my custom python files whose names are variable and decided by input. What I'm trying to do is have my entrypoint send data to other classes for processing.
Folder structure:
root/
├─ dir1/
│ ├─ file1.py
├─ entrypoint.py
├─ dir2/
├─ dir3/
Everything I've tried, except just doing a direct from dir1 import file1
takes 2-3 seconds, which is too long for my usage and makes me think that importlib
is scanning through all other directories before finally finding the correct module.
I've tried this with importlib
:
input_dir = "dir1"
input_file = "file1"
spec = importlib.util.spec_from_file_location(input_file,
f'{os.path.dirname(os.path.abspath(__file__))}/{input_dir}/{input_file}.py')
class_ = importlib.util.module_from_spec(spec)
spec.loader.exec_module(class_)