Configuration management in Python like a Boss(Pydantic with python-dotenv)

Anand Tripathi
Pythonistas
Published in
7 min readFeb 5, 2022

--

Note: For non-members, this article is also available at https://progressstory.com/tech/python/configuration-management-python-pydantic/

<a href=”https://www.freepik.com/vectors/cartoon">Cartoon vector created by vectorjuice — www.freepik.com</a>

The most basic thing of any application is the configuration variables. Some of them are constant throughout the application lifecycle and others keep on changing. The ones that are constants we just create a constant package and reference them from it. It helps us to avoid all the hardcoding in the application. And the other part that is most important is the variables that are kept on changing in the application that is generally known as Configurations.

So how to manage them, where to store them, how to access them? There are many questions related to configuration management. In this blog, we will be going to discuss the ways to manage configuration in Python. Especially the pydantic library

Overview

There are several ways of managing configurations. Let's talk about the most common ones

Constants

Store the configurations as a constant in the application and they will be applied once you start your application. This is the easiest way but the major disadvantage is that whenever you want to change the configuration you have to redeploy the app again. Also to change the configurations you have to change the code every now and then.

File-based Configurations

There are many ways to do it like using a .ini, JSON, XML, YAML file for storing configuration and using libraries like config parser or loading JSON config in frameworks like Django and Flask. But if it comes to adding more configuration or overriding them or putting some logic into it, then it's difficult for them

Environment Variables

This is the most common way nowadays as you just have to load the configuration using environment variables or environment files. It works…

--

--