explaingit

theskumar/python-dotenv

8,759PythonAudience · developerComplexity · 1/5LicenseSetup · easy

TLDR

python-dotenv reads a .env file and loads its KEY=VALUE pairs as environment variables so you can keep API keys and configuration out of your source code.

Mindmap

mindmap
  root((python-dotenv))
    What it does
      Loads .env file
      Sets env variables
    File format
      KEY=VALUE pairs
      Comments and quotes
      Variable substitution
    API
      load_dotenv function
      dotenv_values dict
    Extra tools
      CLI run command
      IPython magic
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Store API keys and database credentials in a .env file so they are never accidentally committed to version control.

USE CASE 2

Load different configuration values for local development versus production without changing your application code.

USE CASE 3

Read a .env file into a Python dictionary and merge it with other configuration sources before passing settings into your app.

USE CASE 4

Run a shell command with .env variables pre-loaded using the dotenv run CLI command.

Tech stack

Python

Getting it running

Difficulty · easy Time to first run · 5min
BSD license, use freely for any purpose, including commercial, with minimal restrictions.

In plain English

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.

Copy-paste prompts

Prompt 1
Show me how to use python-dotenv in a FastAPI app to load a .env file at startup and read variables like DATABASE_URL and SECRET_KEY via os.environ.
Prompt 2
How do I use dotenv_values() to load a .env file into a dictionary and merge it with another config dictionary in Python?
Prompt 3
I want to use python-dotenv locally but have real environment variables take priority in production Docker containers. How do I configure that?
Prompt 4
Show me the .env file syntax for multi-line values, variable substitution with ${OTHER_VAR}, and how to comment out a line.
Prompt 5
How do I use the python-dotenv CLI to add a new variable to my .env file from the terminal and then run my start command with those variables loaded?
Open on GitHub → Explain another repo

← theskumar on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.