# python-dotenv — card for AI agents

Load environment variables from .env files in Python applications.

Category: Dev workflow and config · PyPI package `python-dotenv` · latest version 1.2.4 (checked 2026-11-01)
Install: `pip install python-dotenv`
License: BSD-3-Clause (permissive)

In your own product (generator, app, hosted service): **yes**. Permissive license: ship it inside your own product, closed or open source, and keep its license and copyright notice.

## Rules for AI agents (paste into AGENTS.md or CLAUDE.md)

## python-dotenv
- Install with `pip install python-dotenv`; add the `[cli]` extra only if the `dotenv` command is needed.
- Call `load_dotenv()` once at startup, then read config through `os.environ` or `os.getenv`.
- Do not pass `override=True` unless `.env` values must beat variables already set in the environment; the default is `override=False`.
- Use `dotenv_values(".env")` when you need a dict and must not modify `os.environ`.
- Write variable references as `${VAR}`; bare `$VAR` is not expanded.
- Add `.env` to `.gitignore`, especially when it holds secrets like passwords.
- To stop `load_dotenv()` in production or inside third-party code, set `PYTHON_DOTENV_DISABLED=1` instead of editing calls.

## What it is
- python-dotenv reads key-value pairs from a `.env` file and can set them as environment variables. The maintainers say it helps in developing applications that follow the 12-factor principles. The GitHub repository was created on 2014-09-06 and is BSD-3-Clause licensed. Saurabh Kumar and Bertrand Bonnefoy-Claudet maintain it.
- The problem it addresses is stated in the README. An app that takes its configuration from environment variables is awkward to launch in development, because you have to set those variables yourself. You call `load_dotenv()` once. It looks for a `.env` file next to the script or higher up the directory tree. It then adds each pair to `os.environ`, and the rest of the code reads `os.environ` or `os.getenv` as usual.
- Beyond that one call, it offers `dotenv_values`, which returns a dict and leaves the environment alone. It can also read from a stream instead of a file. It ships an IPython extension and an optional `dotenv` command-line tool.

## Caveats
- The file format has no formal spec: The maintainers say the `.env` format is not formally specified and is still changing. Files should "mostly look like Bash files", but that is a guide, not a guarantee.
- Only ${VAR} is expanded: Variable references must use braces. A bare `$DOMAIN` is left as is.
- Expansion order depends on override: With `override=False`, an expanded variable takes its value from the environment first and from the `.env` file second. With `override=True` or `dotenv_values()`, the file comes first. The same `.env` can therefore produce different values.
- Bare keys behave differently per function: A line with just `FOO` becomes `None` in `dotenv_values`, but `load_dotenv` ignores it. `FOO=` is a different case: it sets an empty string.
- Parsing edge cases are still being fixed: Release v1.2.4 changed how `KEY= # comment` is read. It now parses as an empty string instead of the comment text. The same release fixed `dotenv run --no-override` expansion precedence and a `dotenv get` exit code for empty values.

## Numbers (AgentGid data)
- Gid Score: 80.5 (#92 of all tools, 7 of 299 in its category)
- Used in 568 public projects set up for AI coding agents (9.1%)
- Downloads last week: 184,732,890 (as of 2026-11-24), +12.5% over 4 weeks
- GitHub stars: 8,917 (as of 2026-11-24)
- Last push: 2026-11-09

## Often used with
Pydantic 49.3%, Uvicorn 43.8%, Requests 42.6%, FastAPI 39.3%, OpenAI Python Library 30.6%, PyYAML 29.2%

## Checked alternatives (same job)
- Pydantic Settings: https://agentgid.com/tools/pydantic-settings/agent.md

## Links
- AgentGid page: https://agentgid.com/tools/python-dotenv/
- Repository: https://github.com/theskumar/python-dotenv
- Source: https://github.com/theskumar/python-dotenv#readme
- Source: https://github.com/theskumar/python-dotenv
- Source: https://github.com/theskumar/python-dotenv/releases
- Source: https://saurabh-kumar.com/python-dotenv/


## python-dotenv: keep secrets and settings in a .env file

python-dotenv is a free Python library that reads a small text file named .env and hands its lines to your program as settings. You keep keys, passwords and addresses in that file on your own computer, out of the code. On the server the same program reads the real settings instead.

### Where to use it
- Keep service keys out of code: Your app calls a paid AI or payment service; the secret key sits in .env on your laptop, never in the code you share or push.
- Same code on laptop and server: On your computer settings come from .env; on the hosting service the real settings win, with no code change.
- Read settings without changing anything: A small script reads the values from .env into a plain list of names and values, for example to show which settings are missing.
- Shared and private settings files: The team shares one file with ordinary settings, and each person keeps a second file with their own secrets.
- Run any command with the settings: Start a script, a worker or a test run with the values from .env, without changing that program.
- Notebook experiments with keys: In a Jupyter notebook, load your keys from .env with two lines instead of pasting them into a cell.

### How to start
- Install the package. Its name is python-dotenv; in code you import dotenv.

```
pip install python-dotenv
```

- Load the file at the start of your program, then read settings as usual.

```
from dotenv import load_dotenv

load_dotenv()  # reads variables from a .env file and sets them in os.environ
```

- Put a .env file in the project's root folder and add .env to .gitignore.

```
# Development settings
DOMAIN=example.org
ADMIN_EMAIL=admin@${DOMAIN}
ROOT_URL=${DOMAIN}/app
```


### License
Free to use in any product, paid or closed. If you ship its files, keep the copyright notice and license text with them.

### Good to know
- The .env file is plain text. Keep it out of git and out of anything you share, or the secrets leak with it.
- Settings already set on the computer win over the file, unless you ask for the opposite.
- The file format has no formal standard and still changes a little between versions.
- Loading can be switched off on a server with one setting, PYTHON_DOTENV_DISABLED=1.
- Version 1.2.2 and newer need Python 3.10 or newer.

Checked 2026-11-11 against: https://github.com/theskumar/python-dotenv/blob/main/README.md, https://github.com/theskumar/python-dotenv/blob/main/CHANGELOG.md, https://github.com/theskumar/python-dotenv/blob/main/LICENSE, https://pypi.org/pypi/python-dotenv/json

### Mistakes to avoid
- Don't: load_dotenv() with no path in a script that lives in another folder (a tools/ or scripts/ folder) → Do: load_dotenv("path/to/.env") with an explicit path, or load_dotenv(find_dotenv(usecwd=True)) (Without a path it searches from the script's folder upward; in our run a script in tools/ skipped the .env in the current folder and picked up a different .env two levels higher.)
- Don't: Expecting .env to replace a variable that the shell or server already set → Do: Pass override=True when the file must win (local tools), keep the default on servers (The default keeps existing values; in our run PORT stayed 9000 until override=True.)
- Don't: ROOT_URL=$DOMAIN/app → Do: ROOT_URL=${DOMAIN}/app (Only the braced form is filled in; in our run BARE=$DOMAIN/shop came back unchanged.)
- Don't: if os.environ["DEBUG"]: ... (with DEBUG=false in .env) → Do: os.environ.get("DEBUG", "").lower() in ("1", "true", "yes"), or use pydantic-settings for typed values (Every value is text. In our run DEBUG=true arrived as the string 'true', so even 'false' counts as true.)
- Don't: Committing .env with real keys, or pasting its contents into a chat or an issue → Do: Add .env to .gitignore and commit a .env.example with names and empty values (The file holds secrets in plain text.)

### Questions
- Does it replace settings that are already set on the server? No, not by default. Pass override=True if the file should win.
- Can one value use another, like ${DOMAIN}? Yes, with braces. A bare $DOMAIN stays as written; our run showed the same.
- Where does load_dotenv() look for the file? Next to the Python script and then in the folders above it, not in the folder you started the program from.
- Can a value span several lines? Yes, if it is in single or double quotes.
- Is it still maintained? Yes. Version 1.2.4 came out on 2026-10-01 and more fixes are listed as unreleased.

---
AgentGid's catalog is curated: every entry was checked against the maker's official pages (registry, repository, license and pricing pages) on the date shown; a license value shows the quote it stands on wherever AgentGid has one. Usage numbers come from AgentGid's own scan of public repositories set up for AI coding agents. Claims by makers are reported, not tested by AgentGid.
