0

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_)
messysf
  • 11
  • 1
  • The code you've posted will unconditionally create a new module object and run the contents of `file1.py`, even if `file1.py` has already been loaded by a previous execution of the same code. Is that what you want to happen? (Normal imports don't do that.) – user2357112 Jul 07 '21 at 17:51
  • And why aren't you just doing `module = importlib.import_module(whatever_fully_qualified_name)`? – user2357112 Jul 07 '21 at 17:52
  • Great question @user2357112supportsMonica & an even better observation. I had no idea it would have that kind of effect, I should probably spend some more time in documentation to have it click. I first tried importlib.import_module but it went back to taking 2-3 seconds after calling getattr() to get to the class. I think I just found an answer to how to avoid using getattr() here: https://stackoverflow.com/questions/49434118/python-how-to-create-a-class-object-using-importlib -- gotta test this tomorrow – messysf Jul 08 '21 at 00:36

0 Answers0