Reading yaml configurations in Python
1 min readFeb 27, 2020
I prefer to read configurations in python using yaml file when I have to specify multiple interrelated configurations in one file.
So how to go around it: all you need to do is bucket these configurations under different headers.
Consider the following request_conf.yaml configuration file:
---
- server:
username: 'ram'
url: 'http://localhost:3000/'
- execution:
timeout: 60 #seconds
And here is how to read these configurations in a python file:
import yamldef load_conf_file(config_file):
with open(config_file, "r") as f:
config = yaml.safe_load(f)
server_conf = config[0]["server"]
exec_conf = config[1]["execution"]
return server_conf, exec_confserver_conf,exec_conf = load_conf_file("<path to config file>")username=server_conf["username"]
url=server_conf["url"]timeout=exec_conf["timeout"]
That’s it!
Happy reading!