How to Provide Simple and Clean Global Configuration Variables in Python with YAML
I really despise software that keeps its configuration in the syntax of the language in which the software is written - I’m looking at you, Drupal. Users should not need to know PHP, Python, or Ruby in order to customize settings. That’s why storing settings as YAML is vastly superior since it’s easy for humans to parse at a casual glance. So, what’s the best and most Pythonic way to provide settings globally across a project?
In Python projects, I always ran into a messy problem when trying to share keys and values from YAML across different modules.
An example settings.yaml
:
Together with that YAML dictionary, my first attempt ended up looking something like:
So each time some function needed some values from settings, I ended up passing either multiple arguments or the entire dictionary. This added extra arguments to every function in my code and it was quite ugly. Let’s keep it simple. So, I came up with a much, much cleaner and more Pythonic solution that retains the simplicity of YAML while also honoring The Zen of Python.
Create a file named config.py
and place in it:
Now, everywhere else in your project you can simply import settings like:
That’s it - now your settings can be used anywhere without passing it around as an extra argument.
comments powered by Disqus