tr11 / python-configuration

A Python library to load configuration parameters

Home Page:https://tr11.github.io/python-configuration/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Incosistency between .env and env configuration

bruno-brant opened this issue · comments

.env files are supposed to be a way of defining enviroment variables in a file, to avoid having to set those manually / via script when launching an application. In other words, .env files should have a list of declarations that one would find in the environment itself at any time.

Right now, python-configuration allows us to define the PREFIX and the SEPARATOR of environment keys, allowing us to use envvars to the full potential. However, for dotenv, this is not the case. Variables are mapped linearly, without separation, and the PREFIX is ignored.

dotenv needs to support prefix and separators.

My apologies, are you suggesting that we should remove functionality to match dotenv?

My apologies, are you suggesting that we should remove functionality to match dotenv?

No, of course not! I'm trying to make a case for the exact opposite - that python-configuration should support PREFIX and SEPARATOR for .env files, to make the functionality equivalent.


Let me try to explain what I'm suggesting with an example:

  1. We define the application configuration using a json file. This file is committed to version control and must not contain any sensitive values, although it can (optionally) contain default values for our dev environment, Every variable is declared in it for reference, so that any team member can easily find out how to alter the program.

  2. In production, we use environment variables to control program behavior. Since the variables are primarily defined on a json file, they are hierarchical (of course, we avoid multiple nesting levels to keep it simple, but we still have around 3 levels of nesting). This means using separators to enable the env vars to properly override the values from the .json file. I also use prefixes, to avoid risking any collision with other variables, although I confess this is probably not necessary.

  3. Now, we also need a way to let users configure their local environment. python-configuration is very versatile and many options are available, but we would like to use .env files, as this aligns with the team experience, and it's already how we're supposed to configure the application in production. However, python-configuration support for .env files does not allow the definition of separators and prefixes; therefore, on production I might have define a variable APP__PUBSUB__URL, which would translate to cfg.pubsub.url, but I can't I can't use the .env file to define it, because python-configuration uses different rules for parsing environment variables defined in the actual environment than parsing those in .env.


Now, I would be happy to provide a PR with said functionality if this seems interesting, but I wanted to check if this is something that makes sense to you guys.

Oh, I see! I kept reading Environment instead of dotfiles. This definitely makes sense and would be a great addition. Can you submit a PR to add this functionality? Thanks!

@bruno-brant, can you test the branch in the PR #73 to see if that works for your use case?