You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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?
The text was updated successfully, but these errors were encountered:
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:Currently, if we set the
HOSTNAME
environment variable tolocalhost:8081
, then runload_dotenv()
, then the environment ends up withHOSTNAME=localhost:8081
andURL=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_dotenv
prefers 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:
Suggestion
I would suggest that the variable expansion priority logic follow the same behavior as the general "overriding or not" behavior. More precisely:
override==False
, variable expansion should follow the following priorities:override==True
, variable expansion should follow the currently documented priorities: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?
The text was updated successfully, but these errors were encountered: