Fix ModuleNotFoundError: No module named 'dotenv'
Quick answer
ModuleNotFoundError: No module named 'dotenv' means python-dotenv is not installed in the Python interpreter you are running. The fix is pip install python-dotenv — note the name: the PyPI package is python-dotenv but you import dotenv, and pip install dotenv installs a different, wrong package. If it is still missing, you likely installed it into a different interpreter, so use python -m pip install python-dotenv to target the exact Python you run.
You installed the thing, you import it, and Python still says:
ModuleNotFoundError: No module named 'dotenv'The frustrating part is that many people "installed dotenv" and still see this.
That's because of a naming trap that catches almost everyone: the package you
install and the module you import have different names, and there's a
different package on PyPI literally called dotenv that is the wrong one.
Get the name right and 90% of these vanish. The rest are the classic
ModuleNotFoundError cause — you installed it into a different Python than the one
you're running. This guide fixes both, in that order.
Quick Answer
Install the correct package: pip install python-dotenv (not
pip install dotenv — that's a different, wrong package). Then import it as
from dotenv import load_dotenv. If it's still missing, you installed it into
another interpreter, so run python -m pip install python-dotenv to target the
exact Python you run.
TL;DR
- Package =
python-dotenv, import =dotenv. The names differ by design. pip install dotenvis the wrong package — it won't fix the error.- Still missing? You installed into a different interpreter.
- Use
python -m pip install python-dotenvto target the right Python. - In venvs: activate first, then install.
- In Jupyter/Colab:
%pip install python-dotenv. - Don't name a file
dotenv.py— it shadows the package.
The Naming Trap
This is the single most common reason the error survives a "fix." On PyPI:
| You install | You import | Result |
|---|---|---|
python-dotenv | dotenv | ✅ Works — this is the real package |
dotenv | dotenv | ❌ Wrong package; no load_dotenv |
The distribution name (python-dotenv) and the import name (dotenv) are simply
not the same string — which is common in Python (pip install beautifulsoup4 →
import bs4, pip install opencv-python → import cv2). Memorize this one:
pip install python-dotenv # ✅ correct
# pip install dotenv # ❌ wrong package, don'tIf you already ran pip install dotenv, undo it — it can mask the real fix:
pip uninstall -y dotenv
pip install python-dotenvThen the import works:
from dotenv import load_dotenv
load_dotenv() # reads a .env file into environment variablesStill Missing? It's the Interpreter
If the name is right and it's still not found, you installed it into a different
Python than the one running your script. This is the root cause of nearly every
ModuleNotFoundError, not just this one.
Step 1: Confirm what's installed, and where
pip show python-dotenv # is it installed? note the Location:
python -c "import sys; print(sys.executable)" # which Python am I running?If pip's Python and your script's sys.executable differ, that's the bug — the
package is real, just in the wrong place.
Step 2: Install into the exact interpreter you run
The fix that removes all ambiguity is to invoke pip through the Python you use:
python -m pip install python-dotenvpython -m pipruns pip inside thispython, so the package lands where imports look for it.- If you run
python3, usepython3 -m pip install python-dotenv. - This is more reliable than a bare
pip, whose interpreter can differ frompython.
Step 3: Activate the virtual environment first
Installing before activating puts the package in the global interpreter, which the venv can't see:
# macOS / Linux
source venv/bin/activate
python -m pip install python-dotenv
# Windows (PowerShell)
venv\Scripts\Activate.ps1
python -m pip install python-dotenvAfter activating, which python (macOS/Linux) or where python (Windows) should
point inside the venv.
Step 4: Jupyter, Colab, and IDEs
Notebooks and IDEs often run a different interpreter than your terminal.
# In a Jupyter/Colab cell — %pip targets the kernel's interpreter
%pip install python-dotenv- Jupyter/Colab: use
%pip, then restart the kernel. - VS Code / PyCharm: select the correct interpreter (the one where you installed it) in the interpreter picker, bottom-right / settings.
The Sneaky One: File Shadowing
If everything above is correct and it still fails, check for a file named
dotenv.py in your project. Python searches your own directory first, so your
file wins over the installed package:
# ❌ a local dotenv.py shadows the real package
ls dotenv.py
# ✅ rename it and clear caches
mv dotenv.py load_env.py
rm -rf __pycache__ dotenv.pycThe same applies to a folder named dotenv/ in your project root.
Reproduce and Verify
Confirm the fix end to end:
import dotenv
print(dotenv.__version__) # e.g. 1.0.x — it's importable
from dotenv import load_dotenv
loaded = load_dotenv() # True if a .env was found
print("loaded:", loaded)dotenv.__version__prints → the package is installed in this interpreter.load_dotenv()returnsTruewhen it finds a.envfile.
Green state: pip show python-dotenv and your sys.executable point at the
same environment, import dotenv succeeds, and load_dotenv() returns True
with your .env present.
Framework Notes
- Flask / FastAPI / Django: add
python-dotenvtorequirements.txt(orpyproject.toml) so deploys install it. Missing from requirements is the #1 "works locally, breaks in prod" cause. - Docker: install it in the image, not just your host —
RUN pip install python-dotenv(or via requirements). A missing entry means the container has nodotenv. - Poetry / uv / Pipenv: use
poetry add python-dotenv,uv add python-dotenv, orpipenv install python-dotenvso the lockfile records it. - CI/CD: ensure the pipeline installs from the same
requirements.txtyour app expects; a partial cache can skip it.
Prevention
- Always
pip install python-dotenv— neverdotenv. - Prefer
python -m pip installto pin the interpreter. - Pin
python-dotenvinrequirements.txt/ lockfile. - Activate the venv before installing.
- In notebooks, install with
%pip. - Never name a file or folder
dotenv. - Check
sys.executablewhen a "installed" package won't import.
Troubleshooting Matrix
| Symptom | Likely cause | Fix |
|---|---|---|
| Never installed | Missing package | pip install python-dotenv |
Installed dotenv, still fails | Wrong package | Uninstall dotenv, install python-dotenv |
| Installed but not found | Wrong interpreter | python -m pip install python-dotenv |
| Works in terminal, not IDE | IDE uses another interpreter | Select the right interpreter |
| Fails in Jupyter | Kernel differs from terminal | %pip install python-dotenv |
| Works locally, breaks in prod | Not in requirements | Add to requirements.txt |
| Import finds nothing useful | Local dotenv.py shadowing | Rename the file |
Related Guides
- Resolve ModuleNotFoundError: No module named 'cv2'
- Fix ImportError: cannot import name 'Mapping' from 'collections'
- Resolve ValueError: too many values to unpack
- Fix TypeError: Object of type datetime is not JSON serializable
- Setting up clean Python virtual environments
External References
- python-dotenv on PyPI
- python-dotenv documentation (GitHub)
- Python — Modules and the import system
- pip — Installing packages
FAQs
Is dotenv a real package on PyPI?
Yes — and that's the trap. There is a separate package literally named dotenv,
but it is not the one that provides load_dotenv. You want python-dotenv.
Installing dotenv is the most common reason the error survives.
Why is the import name different from the install name?
It's a common Python convention — the distribution name and the top-level module
don't have to match. Other examples: beautifulsoup4 → bs4, Pillow → PIL,
opencv-python → cv2. When in doubt, check the package's PyPI page for the
import name.
How do I know which interpreter my code uses?
import sys; print(sys.executable) prints the exact Python running your code.
Compare it to pip show python-dotenv's Location. If they don't match, install
with python -m pip.
Do I need python-dotenv in production?
Only if you load .env files there. Many teams load real environment variables
directly in production and use python-dotenv for local dev — but if your code
calls load_dotenv() anywhere that runs in prod, the package must be installed
there too.
Can I avoid the error by not using .env files?
You can read environment variables with os.environ / os.getenv without
python-dotenv at all. python-dotenv is a convenience for loading a .env file
into os.environ; if you set real env vars another way, you don't need it.
Key takeaways
- •The PyPI package is python-dotenv, but you import it as dotenv — the names differ on purpose.
- •pip install dotenv installs a different, unrelated package and does NOT fix the error — always use python-dotenv.
- •If it is still missing after installing, you installed it into a different interpreter than the one you run.
- •Use python -m pip install python-dotenv to guarantee it lands in the interpreter you are using.
- •A local file named dotenv.py shadows the package — rename it.
- •In Jupyter or Colab, install with %pip install python-dotenv so it targets the kernel.
Frequently asked questions
What is the correct pip install for dotenv?
pip install python-dotenv. The distribution on PyPI is named python-dotenv, and it provides the dotenv module you import with from dotenv import load_dotenv. Do not run pip install dotenv — that installs a different, unrelated package that does not provide load_dotenv.
Why does pip install dotenv not fix the error?
Because dotenv is a separate PyPI package, not python-dotenv. It installs successfully but does not provide the load_dotenv function, so from dotenv import load_dotenv still raises ImportError or the module error persists. Uninstall it and install python-dotenv instead.
I installed python-dotenv but still get No module named 'dotenv' — why?
You almost certainly installed it into a different Python than the one running your code. Run python -m pip install python-dotenv so pip targets the exact interpreter you use, and check that sys.executable matches the Python where pip installed the package.
How do I fix it in a virtual environment?
Activate the virtual environment first, then install inside it: source venv/bin/activate (or venv\Scripts\activate on Windows), then python -m pip install python-dotenv. Installing before activating puts the package in the global interpreter, which the venv does not see.
How do I fix No module named 'dotenv' in Jupyter or Colab?
Run %pip install python-dotenv inside a notebook cell. The %pip magic installs into the kernel's interpreter, unlike a plain pip in a terminal that may target a different Python. Restart the kernel afterward if the import still fails.
Why should I not name my file dotenv.py?
Because Python searches your own directory first, so import dotenv imports your local dotenv.py instead of the installed package — and your file has no load_dotenv. Rename the file to something like config.py or load_env.py and delete any dotenv.pyc or __pycache__ leftovers.