Skip to content

Inconsistency in priorities of variable expansion #282

Description

@mathieu1

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions