I use python files which I load “manually” into a specific module for configuration, so that python code can get executed as part of the configuration.
In the program that uses the configuration I do something like:
import importlib modname = "__config__" spec = importlib.util.spec_from_file_location(modname, "config1.py") mod = importlib.util.module_from_spec(spec) spec.loader.exec_module(mod) # get config from vars(mod)
What I am wondering is if it is somehow possible to kind of incrementally add to the module by adding more from a second and third config file? In this case it would be great if the
exec_module call could already see which objects already are in the module so executing the code from the second file would be able to see the objects from the first file.
I need to be able to do this in a way where objects from file 1 can be accessed after the first config file is processed and before the second one is processed, so it is not just a matter of using a loader to combine file 1 and 2 and then create the final module in one go from both.