python-dotenv
Reads key-value pairs from .env files into os.environ for Python apps, respecting existing variables.
Official Documentation ↗v1. Check the official docs for the latest API surface.Installation
pip install python-dotenvMethods
No methods match this filter.
Reads key-value pairs from .env files into os.environ for Python apps, respecting existing variables.
Overview
python-dotenv is the Python counterpart of the dotenv convention: a committed .env.example documents keys, a gitignored .env holds local values, and load_dotenv merges them into os.environ before configuration is read.
The loader respects real environment precedence by default, and dotenv_values supports tooling that inspects files without side effects. It pairs naturally with pydantic-settings or environ-validation layers, which turn raw strings into typed, checked configuration.
Core Methods
The most frequently used entry points, with signatures and runnable examples:
load_dotenv— Loads variables from .env into os.environ without replacing values that already exist.dotenv_values— Returns the parsed file as a dict without modifying os.environ, for inspection and validation.find_dotenv— Searches upward from the file importing it for a .env file; usecwd starts from the current directory instead.
Usage Notes
- Load early, before settings modules evaluate, then pass values through typed config objects.
- Keep .env.example current; it is the contract new contributors rely on.
- Use dotenv_values in tests to assert which keys a deployment expects.
Common Pitfalls
- Values are strings: ints and booleans need explicit conversion or schema coercion.
- Quoting matters: unquoted spaces and # comments can truncate values unexpectedly.
- Loading multiple times with different paths is order-dependent; keep one load site.
Security notes
Quoted verbatim from the official documentation; this is not a complete threat model. 来源:https://cdn.jsdelivr.net/gh/theskumar/python-dotenv@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:52:03.851Z.
- You will probably want to add .env to your .gitignore, especially if it contains secrets like a password. (https://cdn.jsdelivr.net/gh/theskumar/python-dotenv@latest/README.md)
- Allowed escape sequences: - in single-quoted values: \, ' - in double-quoted values: \, ', ", \a, \b, \f, \n, \r, \t, \v ### Multiline values It is possible for single- or double-quoted values to span multiple lines. (https://cdn.jsdelivr.net/gh/theskumar/python-dotenv@latest/README.md)
FAQ
- Why are my environment variables overriding the file?
- That is by design: load_dotenv never replaces existing variables, so deployment platforms win. Pass override=True only when the file should take precedence, such as in local tooling runs.
- How do Flask or Django integrate with it?
- Call load_dotenv() before creating the app or settings object. Flask auto-loads .env when python-dotenv is installed; Django projects typically call it at the top of settings.py before reading os.environ.
- How do I validate required keys at startup?
- Use dotenv_values or read os.environ after loading, then check against a required list or a validator such as pydantic-settings so a missing key fails immediately with a precise message.
Report a problem with this API docOpens a pre-filled issue on GitHub.