How to Load Config Files into Python Scripts
The aim of this page📝 is to sketch a bunch of issues related to configuration, i.e. the “contextualization” of deployments…but ending up with loading a static config file into a python script
2 min readSep 27, 2022
An app’s config is everything that is likely to vary between deploys (staging, production, developer environments, etc.)
— The Twelve-Factor App > III. Config
… sometimes on user-level (credentials, emails, etc.)
IT IS COMPLICATED…
- A typical way of setting config/injecting values into applications is via config files
- Think HAProxy, the widely used load balancer utilizing
haproxy.cfg
- Think Apache Web server utilizing
.htaccess
- Somehow, however, those config files have to get there
- There are various ways such as using package managers and/or
- …manually changing config files
- ….checking them in the source control
- ….using config management tools (chef/puppet)
- ….using reactive KV stores like Hashicorp Consul instead (yeah!)
- ….using environmental variables instead as per The Twelve-Factor App > III. Config, the config should be stored in the environment. Still, somehow, it has to get there, and for sure, you cannot manipulate the client’s environmental variables persistently
JUST GET ME A SIMPLE STATIC CONFIG FILE INTO A PYTHON SCRIPT PLEASE
- For a small-scale & small-team CLI apps, perhaps, a static config file checked into the source control is all that’s required
- Import the module => use it to load the content of the file => use the loaded file as a dictionary to access your values
- What follows is a tiny example of having a static config file load into a python program in two typical formats:
yaml
andjson
- This is, admittedly, a violation of 12factor, which
requires strict separation of config from code. Config varies substantially across deploys, code does not.A litmus test for whether an app has all config correctly factored out of the code is whether the codebase could be made open source at any moment, without compromising any credentials._
# CONFIG.YAML
EMAIL_ADDRESS = pkutaj@gmail.com
———————————————————————————————————————————————————————————————>
# SCRIPT.PY
import yaml
with open("config.yml", "rt") as config_file:
config = yaml.load(config_file)
EMAIL_ADDRESS = config['EMAIL_ADDRESS']
>>> "pkutaj@gmail.com"
- For a really short config, I prefer to use
json
format — it is becauseyaml
is not a built-in library and you, therefore, need to import the module via pip manually
# CONFIG.JSON
{
"EMAIL_ADDRESS": "pkutaj@gmail.com"
}
———————————————————————————————————————————————————————————————>
# SCRIPT.PY
import json
with open("config.json") as config_file:
config = json.load(config_filie)
EMAIL_ADDRESS = config['EMAIL_ADDRESS']
>>> "pkutaj@gmail.com"