Repository navigation
Description
Behavior
python-dotenv priorities are correctly documented, but inconsistent when loading a .env file with internal dependencies in variable expansion.
Consider the following .env file:
HOSTNAME=localhost:8080
URL=http://${HOSTNAME}
Currently, if we set the HOSTNAME environment variable to localhost:8081, then run load_dotenv(), then the environment ends up with HOSTNAME=localhost:8081 and URL=http://localhost8080, which at first is surprising.
Underlying reason
The reason for this is that (by default) load_dotenv() does not override set environment variables, but when doing variable expansion inside a given file, load_dotenvprefers the variable as set in the file than from the environment (as documented in https://github.com/theskumar/python-dotenv#usages).
Workaround
A workaround consists of changing the priorities in the file explicitly, as in this example:
HOSTNAME=${HOSTNAME:-localhost:8080}
URL=http://${HOSTNAME}
Suggestion
I would suggest that the variable expansion priority logic follow the same behavior as the general "overriding or not" behavior. More precisely:
- when
override==False, variable expansion should follow the following priorities:- Value of that variable in the environment.
- Value of that variable in the .env file.
- Default value, if provided.
- Empty string.
- when
override==True, variable expansion should follow the currently documented priorities:- Value of that variable in the .env file.
- Value of that variable in the environment.
- Default value, if provided.
- Empty string.
It would consist of a breaking change in behavior, but it seems that in the case it does matter, the current behavior is probably not what is expected.
Is that a change you'd be willing to accept?
Activity
bbc2 commentedon Nov 15, 2020
I think that you suggestion makes sense. I've given it a go in #287. Would it solve your issue?