I would like to load a .py file at runtime. This .py file is basically a config file with the following format:
var1=value
var2=value
predicate_function=func line : <return true or false>
Once this file is loaded, I would like to be able to access var1, var2 and predicate_function. For each line, I’ll pass it to the predicate function, and if it returns false, I’ll ignore it.
In any case, I’m not sure how to load a python file at runtime and access its variables.
Clarification: there may be any number of these config files that I need to pass to the main program and I won’t know their names until runtime. Google tells me I should use __import__. I’m not sure how to correctly use that method and then access the variables of the imported file.
You just need to be able to dynamically specify the imports and then dynamically get at the variables.
Let’s say your config file is bar.py and looks like this:
Then your code should look like this:
I get this output:
Then you at least have all the variables and functions. I didn’t quite get what you wanted from the predicate functions, but maybe you can get that on your own now.