I need to store configurations (key/value) for a Python application and I am searching for the best way to store these configurations in a file.
I run into Python\'s
Consider using plain Python files as configuration files.
An example (config.py):
# use normal python comments
value1 = 32
value2 = "A string value"
value3 = ["lists", "are", "handy"]
value4 = {"and": "so", "are": "dictionaries"}
In your program, load the config file using exec (docs):
from pathlib import Path
if __name__ == "__main__":
config = {}
exec(Path("config.py").read_text(encoding="utf8"), {}, config)
print config["value1"]
print config["value4"]
I like this approach, for the following reasons:
The approach is widely used, a few examples:
execfile, it uses import to read/execute settings.py AFAIK, but the end result is the same: the code inside the settings file is executed.~/.bashrc on startup.site.py on startup.