Store API keys and database credentials in a .env file so they are never accidentally committed to version control.
Load different configuration values for local development versus production without changing your application code.
Read a .env file into a Python dictionary and merge it with other configuration sources before passing settings into your app.
Run a shell command with .env variables pre-loaded using the dotenv run CLI command.
python-dotenv is a Python library that reads a file named .env in your project directory and loads its contents as environment variables. The .env file holds configuration values like API keys, database addresses, or feature flags in simple KEY=VALUE format. This is a common pattern for keeping configuration separate from code, and python-dotenv makes it work automatically in local development without needing to set variables manually each time. The main function is load_dotenv(), which you call at the start of your Python application. It searches up the directory tree for a .env file, reads each line, and adds the variables to the environment. By default it does not overwrite variables that are already set in the environment, so real environment variables on a server take priority over the .env file. A separate function, dotenv_values(), reads the same file but returns a plain Python dictionary instead of modifying the environment, which is useful when you want to combine settings from multiple sources before passing them into your code. The .env file format is similar to Bash: you can use quotes around values, write comments with #, reference other variables with ${VARIABLE} syntax, and span long values across multiple lines. A command-line tool is also included for reading, setting, and listing .env variables from a terminal without opening the file by hand. You can also run a command with the loaded variables using dotenv run. IPython support is included, letting you load a .env file in a notebook session with a magic command. The library can be disabled entirely by setting an environment variable, which is useful when deploying to environments where you want to prevent accidental loading.
← theskumar on gitmyhub — every repo by this author, as a profile.
Verify against the repo before relying on details.