← All posts

ModuleNotFoundError: No module named 'dotenv' (How to Fix)

Published

You wrote from dotenv import load_dotenv and Python replied:

ModuleNotFoundError: No module named 'dotenv'

The fix for most people is one line:

pip install python-dotenv

The package is called python-dotenv on PyPI, but you import it as dotenv. That mismatch causes the majority of hits on this error. If you ran pip install dotenv instead, you installed a different, unrelated package, and the import still fails. Uninstall it and install the right one:

pip uninstall dotenv
pip install python-dotenv

If that fixed it, you are done. If not, the cause is one of the four below.

Cause 2: installed in a different Python than the one running your script

You have more than one Python on the machine, and pip belongs to one while python runs another. Symptoms: pip install python-dotenv says it is already installed, the import still fails.

Install using the exact interpreter that runs your code:

python -m pip install python-dotenv

Or if you run scripts with python3:

python3 -m pip install python-dotenv

python -m pip guarantees pip and the interpreter match. Prefer it over bare pip always.

Cause 3: virtual environment not activated

You installed the package globally but your project uses a venv, or the other way round. Check what is active:

which python

If it does not point into your project's .venv or venv folder, activate it first:

source .venv/bin/activate

On Windows:

.venv\Scripts\activate

Then install inside it: python -m pip install python-dotenv.

The same thing happens in VS Code and PyCharm if the editor's selected interpreter is not the one your venv uses. Check the interpreter picker in the bottom bar.

Cause 4: your file is named dotenv.py

If there is a file called dotenv.py in your project directory, Python imports that instead of the installed package, and it has no load_dotenv. The error message changes slightly to an ImportError about load_dotenv specifically, but people land here for it. Rename the file.

Also check for a dotenv/ folder or a stale __pycache__ in your project.

Cause 5: Docker or CI without the dependency

Works locally, fails in the container or pipeline. The package is not in requirements.txt or pyproject.toml, so the build never installs it. Add it:

python-dotenv>=1.0

Then rebuild.

Confirm it is installed

python -c "import dotenv; print(dotenv.__file__)"

That prints the path if the import works, and shows you which interpreter found it.

Using it once it works

from dotenv import load_dotenv
import os

load_dotenv()

api_key = os.environ["API_KEY"]

load_dotenv() reads a .env file from the current directory or its parents and puts the values into os.environ. If you are new to the format, what is a .env file covers it, and Python environment variables covers reading and validating them properly.

One habit worth building at the same time: add .env to .gitignore before you commit anything. The reason people use dotenv is to keep secrets out of code, and committing the file undoes that.

FAQ

Is it dotenv or python-dotenv? Install python-dotenv. Import dotenv. The names differ and that is the whole problem.

I installed python-dotenv and still get the error. Wrong interpreter. Use python -m pip install python-dotenv with the same python that runs your script, and check your venv is activated.

Does Python 3.12 or 3.13 change anything? No. python-dotenv supports current Python versions. The error is about installation, not version.

Do I need dotenv in production? Usually not. Production platforms set environment variables directly. load_dotenv() quietly does nothing if there is no file, so leaving the call in is harmless, but the package still needs to be installed for the import to succeed.