importpkl

How to import module from an external property in pickle


I wanted to inject a filename into a pkl module through a property so I could do something like

base = import(read("prop:config-file"))

But I get an error... Is there any way to achieve this? I'm thinking about adding a placeholder on the import parameter and turn the .pkl file into a some sort of template, but it seems like a hack. I would prefer to follow the best recommendations to do this. Thanks


Solution

  • Afaik, you can't pre-evaluate the imported file's name. But you could (if reasonable) import all possible import files using globbed imports with import* into a mapping, and then index into the right one:

    base = import*("possible_imports/*.pkl")[read("prop:config-file")]