Dotenv-CLI is a simple package that provides the dotenv
command. It reads the
.env
file from the current directory puts the contents in the environment and
executes the given command.
dotenv
supports alternative .env
files like .env.development
via the -e
or --dotenv
parametes.
dotenv
provides bash completion, so you can use dotenv
like this:
$ dotenv make <TAB>
all clean docs lint release test
dotenv-cli is available on PyPi, you can install it via:
$ pip install dotenv-cli
Alternatively, you can install dotenv-cli on Debian based distributions via:
# apt-get install python3-dotenv-cli
Create an .env
file in the root of your project and populate it with some
values like so:
SOME_SECRET=donttrythisathome
SOME_CONFIG=foo
Just prepend the command you want to run with the extra environment variables
from the .env
file with dotenv
:
$ dotenv some-command
and those variables will be available in your environment variables.
The parser understands the following:
- Basic unquoted values (
BASIC=basic basic
) - Lines starting with
export
(export EXPORT=foo
), so you cansource
the file in bash - Lines starting with
#
are ignored (# Comment
) - Empty values (
EMPTY=
) become empty strings - Inner quotes are maintained in basic values:
INNER_QUOTES=this 'is' a test
orINNER_QUOTES2=this "is" a test
- White spaces are trimmed from unquoted values:
TRIM_WHITESPACE= foo
and maintained in quoted values:KEEP_WHITESPACE=" foo "
- Interpret escapes (e.g.
\n
) in double quoted values, keep them as-is in single quoted values.
Example .env
file:
BASIC=basic basic
export EXPORT=foo
EMPTY=
INNER_QUOTES=this 'is' a test
INNER_QUOTES2=this "is" a test
TRIM_WHITESPACE= foo
KEEP_WHITESPACE=" foo "
MULTILINE_DQ="multi\nline"
MULTILINE_SQ='multi\nline'
MULTILINE_NQ=multi\nline
#
# some comment
becomes:
$ dotenv env
BASIC=basic basic
EXPORT=foo
EMPTY=
INNER_QUOTES=this 'is' a test
INNER_QUOTES2=this "is" a test
TRIM_WHITESPACE=foo
KEEP_WHITESPACE= foo
MULTILINE_DQ=multi
line
MULTILINE_SQ=multi\nline
MULTILINE_NQ=multi\nline