There are multiple posts discussing whether this is recommended, but lets suppose you want to do a config.py
for your python app. Is there any difference between the two (similar to me) approaches below:
- 1). make the module as an ini file and then parse it with
exec(Path())
Like the following example, taken from here:
## config.py ##
value1 = 32
value2 = "A string value"
value3 = ["lists", "are", "handy"]
value4 = {"and": "so", "are": "dictionaries"}
and then
from pathlib import Path
if __name__ == "__main__":
config = {}
exec(Path("config.py").read_text(encoding="utf8"), {}, config)
print config["value1"]
print config["value4"]
- 2). write a module with just a dictionary with the settings and then import the module:
Hence:
## config.py ##
DEFAULT = {
value1: 32,
value2: "A string value",
value3: ["lists", "are", "handy"],
value4 : {"and": "so", "are": "dictionaries"},
}
and then
import config.py
if __name__ == "__main__":
cfg = config.DEFAULT
print cfg["value1"]
print cfg["value4"]