Skip to content
Agents tracked: 506 Releases (7d): 634 Agent status: 4 with issuesSince last limit reset: Claude 15d · Codex 0d For AI agents: Markdown Updated Nov 26, 2026

python-dotenv

Keep secrets and settings in a .env file.

Dev workflow and configPYPI

GitHub

1. License

“Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met”

Any use is allowed; keep the copyright notice and license text when you ship its files.

github.com/theskumar/python-dotenv/blob/main/LICENSE

From the package registry and GitHub, checked daily; card checked Nov 11, 2026. Tap a verdict to see its source.How we checkedReport an error

What python-dotenv does

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.

What people build with 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.

python-dotenv popularity

80 / 100
Gid Score, #92 overall
185M
downloads a week, pypi
8,917
GitHub stars
568
agent projects use it
python-dotenv: downloads per weekPyPI, python-dotenv 182,542,712
python-dotenv: GitHub starstheskumar/python-dotenv 8,917

Gid Score measures public popularity and activity, not quality. How the Gid Score is calculated

How python-dotenv is used

  • python-dotenv is a dependency in 568 of the 6,258 public GitHub projects set up for AI coding agents that we scanned (35.0% of the 1,623 Python ones), #3 of 299 in dev workflow and config.
  • Counting the projects our scan samples by dependency (Solana 3,067, Ethereum and EVM 1,774, 3D and games 3,130, agent frameworks and SDKs 13,750), it appears in 5,334; those are left out of every share on this page.
  • The most used alternative in our scan is Pydantic Settings (219 projects).
  • Projects that use it most often also use Pydantic (49.3%), Uvicorn (43.8%), Requests (42.6%).
  • It is more common in projects set up for OpenClaw (12.0%) than for OpenAI Codex (4.3%).

By coding agent

286 projects, 9.4%
240 projects, 7.3%
192 projects, 7.9%
40 projects, 12.0%
25 projects, 5.3%
23 projects, 8.2%
20 projects, 4.3%
15 projects, 4.4%

Often used with

In 49.3% of its projects
In 43.8% of its projects
In 42.6% of its projects
In 39.3% of its projects
In 30.6% of its projects
In 29.2% of its projects
In 28.7% of its projects
In 29.6% of its projects

python-dotenv in practice

Strengths

Real environment variables win by default

<code>load_dotenv()</code> does not override variables that are already set, because it defaults to <code>override=False</code>. A <code>.env</code> file can sit in development while deployed values stay in charge. Pass <code>override=True</code> to reverse this.

dotenv_values gives you a dict instead

<code>dotenv_values</code> parses the file and returns a <code>dict</code> without touching <code>os.environ</code>. The README shows it merging <code>.env.shared</code>, <code>.env.secret</code> and <code>os.environ</code> into one config.

Config does not have to come from a file

Both <code>load_dotenv</code> and <code>dotenv_values</code> take a <code>stream</code> argument. The docs give the network as an example source. The file format section adds that reading from FIFOs (named pipes) on Unix is supported.

An off switch for code you cannot edit

Setting <code>PYTHON_DOTENV_DISABLED=1</code> stops <code>load_dotenv()</code> from loading anything. The docs suggest this for production, or when a third-party package calls it.

Watch out for

The file format has no formal spec

The maintainers say the <code>.env</code> 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 <code>$DOMAIN</code> is left as is.

Expansion order depends on override

With <code>override=False</code>, an expanded variable takes its value from the environment first and from the <code>.env</code> file second. With <code>override=True</code> or <code>dotenv_values()</code>, the file comes first. The same <code>.env</code> can therefore produce different values.

Layer shared, secret and real environment values

import os
from dotenv import dotenv_values

config = {
    **dotenv_values(".env.shared"), # load shared development variables
    **dotenv_values(".env.secret"), # load sensitive variables
    **os.environ, # override loaded values with environment variables
}

More about python-dotenv

Head to head
Packages around it
  • requests: HTTP client
  • pydantic: Data validation
  • uvicorn: ASGI server
  • fastapi: Web API framework
  • solders: Solana Python bindings
  • numpy: Numerical arrays
More in Dev workflow and config

Alternatives

python-dotenv This page

BSD-3-ClauseGid 80185M/wk

Pydantic Settings

Pick Pydantic Settings when you also want each setting checked and turned into the right type (number, true/false) when the app starts.

MITGid 71123M/wk

environs

Pick environs when you want .env loading plus typed helpers such as reading a number, a list or a URL from a setting.

python-decouple

Pick python-decouple when settings may come from either .env or an .ini file and you want defaults and casting in one call.

Compare python-dotenv with another tool

Get started

Tell your AI assistant

“Use python-dotenv (pin python-dotenv==1.2.4, Python 3.10+) to load settings from a .env file with an explicit path at startup, read keys with os.environ[...], add .env to .gitignore and commit a .env.example with empty values.”

  1. Install the package. Its name is python-dotenv; in code you import dotenv.

    pip install python-dotenv

    From github.com/theskumar/python-dotenv/blob/main/README.md

  2. 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
    

    From github.com/theskumar/python-dotenv/blob/main/README.md

  3. 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
    

    From github.com/theskumar/python-dotenv/blob/main/README.md

Rules for your agent

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.

Mistakes to avoid

load_dotenv() with no path in a script that lives in another folder (a tools/ or scripts/ folder)
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.

Expecting .env to replace a variable that the shell or server already set
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.

ROOT_URL=$DOMAIN/app
ROOT_URL=${DOMAIN}/app

Only the braced form is filled in; in our run BARE=$DOMAIN/shop came back unchanged.

if os.environ["DEBUG"]: ... (with DEBUG=false in .env)
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.

Committing .env with real keys, or pasting its contents into a chat or an issue
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.