How to Install python-dotenv in Python
Read key-value pairs from a .env file and set them as environment variables
pip install python-dotenv
What is python-dotenv?
Read key-value pairs from a .env file and set them as environment variables
python-dotenv reads key-value pairs from a file and can set them as environment variables. It helps in the development of applications following the 12-factor principles.
- Getting Started - Other Use Cases - Load configuration without altering the environment - Parse configuration as a stream - Load .env files in IPython - Command-line Interface - File format - Multiline values - Variable expansion - Related Projects - Acknowledgements
If your application takes its configuration from environment variables, like a 12-factor application, launching it in development is not very practical because you have to set those environment variables yourself.
Quick Start
Minimal example to get started with python-dotenv:
from dotenv import load_dotenv
import os
load_dotenv() # reads .env file in current directory
api_key = os.environ.get("API_KEY")
print(api_key)
# .env file contents:
# API_KEY=your-secret-key
# DEBUG=true
Installation
pip (standard)
pip install python-dotenv
Virtual environment (recommended)
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install python-dotenv
pip3
pip3 install python-dotenv
conda
conda install -c conda-forge python-dotenv
Poetry
poetry add python-dotenv
Verify the Installation
After installing, confirm the package is available:
python -c "import dotenv; print(dotenv.__version__)"
If this prints a version number, installation succeeded. If you see a ModuleNotFoundError, see the errors section below.
Installation Errors
Common errors when installing python-dotenv with pip.
ModuleNotFoundError: No module named 'dotenv'
Cause: The package is not installed in the current Python environment.
Fix: Run pip install python-dotenv. If using a virtual environment, ensure it is activated first.
ModuleNotFoundError: No module named 'dotenv' (installed but still failing)
Cause: pip installed the package into a different Python than the one running your script.
Fix: Use python -m pip install python-dotenv to install into the interpreter you are running.
ImportError: cannot import name 'X' from 'dotenv'
Cause: The function or class does not exist in the installed version.
Fix: Check the version with pip show python-dotenv and upgrade with pip install --upgrade python-dotenv.
pip: command not found
Cause: pip is not in PATH or Python was not added to PATH during installation.
Fix: Try python -m pip install python-dotenv. On macOS/Linux try pip3.
PermissionError: [Errno 13] Permission denied
Cause: No write access to the system Python package directory.
Fix: Use a virtual environment, or add --user: pip install --user python-dotenv
SSL: CERTIFICATE_VERIFY_FAILED
Cause: pip cannot verify PyPI's SSL certificate — common behind corporate proxies.
Fix: Try: pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org python-dotenv
Recent Releases
| Version | Released |
|---|---|
1.2.2 latest |
2026-03-01 |
1.2.0 |
2025-10-26 |
1.2.1 |
2025-10-26 |
1.1.1 |
2025-06-24 |
1.1.0 |
2025-03-25 |
Manage python-dotenv
Upgrade to latest version
pip install --upgrade python-dotenv
Install a specific version
pip install python-dotenv==1.2.2
Uninstall
pip uninstall python-dotenv
Check what is installed
pip show python-dotenv