| icon | lucide/file-text |
|---|
The format is not formally specified and still improves over time. That being
said, .env files should mostly look like Bash files. Reading from FIFOs (named
pipes) on Unix systems is also supported.
Keys can be unquoted or single-quoted. Values can be unquoted, single-quoted, or double-quoted. Spaces before and after keys, equal signs, and values are ignored.
# Unquoted
USER=foo
# Single-quoted key and value
'USER'='foo'
# Double-quoted value
USER="foo"Lines can start with the export directive, which does not affect their
interpretation:
export USER=fooLines starting with # are comments:
# This is a comment
USER=fooUnquoted values can have inline comments after whitespace:
USER=foo # this is a commentInline comments are not supported in quoted values. The # becomes part of
the value:
USER="foo # this is NOT a comment"In single-quoted values: \\, \'
In double-quoted values: \\, \', \", \a, \b, \f, \n, \r, \t, \v
Unquoted values do not support escape sequences.
Single- or double-quoted values can span multiple lines. The following are equivalent:
FOO="first line
second line"FOO="first line\nsecond line"Unquoted values cannot span multiple lines.
A variable can have no value:
FOOdotenv_values returns None for such keys (e.g. {"FOO": None}).
load_dotenv ignores them.
This is different from FOO=, which sets the variable to an empty string.
python-dotenv can interpolate variables using POSIX variable expansion.
Variables must use the ${VAR} syntax with braces:
DOMAIN=example.org
ADMIN_EMAIL=admin@${DOMAIN}Bare variables like $DOMAIN (without braces) are not expanded.
Default values are supported with ${VAR:-default}:
DATABASE_HOST=${DB_HOST:-localhost}
DATABASE_URL=postgres://${DATABASE_HOST}:5432/mydbIf DB_HOST is not defined in the .env file or the environment, DATABASE_HOST
resolves to localhost.
With load_dotenv(override=True) or dotenv_values(), the value of a variable
is the first of the values defined in the following list:
- Value of that variable in the
.envfile. - Value of that variable in the environment.
- Default value, if provided.
- Empty string.
With load_dotenv(override=False) (the default), the value of a variable is
the first of the values defined in the following list:
- Value of that variable in the environment.
- Value of that variable in the
.envfile. - Default value, if provided.
- Empty string.
!!! note
`dotenv_values()` does not have an `override` parameter and always resolves
variables using the `override=True` order above.