My favorite way to configure a Python project

Kevin Tewouda
5 min readJan 7, 2024

Pydantic-settings at our rescue

Photo by Ferenc Almasi on Unsplash

The first thing I want to note is that the following recommendations are valid for web applications or API services. For other types of applications like Command Line Interfaces, they might not be the best suit, even if I think they are also relevant.

Also, what I mean by configuration are things that can be changed between deploys or environments like:

  • database server URLs
  • secret key used to sign cookies
  • credentials to connect to an external service
  • etc…

I don’t include internal application configurations like route URLs that should be handled directly in code.

The are various ways to configure your application. We will see the most common and the one I use and how I use it.

Configuration with files

A common approach I see in various projects is to use files to configure the application. In Python, many formats are supported directly via the standard library or through third packages. The common ones are:

--

--

Kevin Tewouda

Déserteur camerounais résidant désormais en France. Passionné de programmation, sport, de cinéma et mangas. J’écris en français et en anglais dû à mes origines.