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?
Behavior
python-dotenv priorities are correctly documented, but inconsistent when loading a .env file with internal dependencies in variable expansion.
Consider the following
.envfile:Currently, if we set the
HOSTNAMEenvironment variable tolocalhost:8081, then runload_dotenv(), then the environment ends up withHOSTNAME=localhost:8081andURL=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:
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?