Demo, all content is generated
Question

ModuleNotFoundError: No module named 'dotenv' but pip says it's installed??

Solved · 2601 views · asked by jakeypoo · edited
$ pip install python-dotenv
Requirement already satisfied: python-dotenv in /opt/homebrew/lib/python3.12/site-packages
$ python main.py
ModuleNotFoundError: No module named 'dotenv'

Also the Run button in Cursor gives the same. Losing my mind a bit.

What I’ve tried

Installed it again, installed "dotenv" too (other package), restarted Cursor.

Comment
What do which python and which pip print? marco_py · edited

3 answers

Marked as helpful by the asker
marco_py · edited

Your pip and your python are different Pythons. Check:

which python; which pip
python -c "import sys; print(sys.executable)"

Fix it for good with a virtual environment per project:

python3 -m venv .venv
source .venv/bin/activate
python -m pip install python-dotenv

Always install with python -m pip, then it goes to the Python you run. In Cursor: Cmd+Shift+P → "Python: Select Interpreter" → pick .venv, so the Run button uses it too. And uninstall that other dotenv package, it's not the one you want.

Comment
which python pointed to some old anaconda thing i installed in 2021. venv fixed it. thank you so much jakeypoo · edited
python -m pip should be the first thing anyone learns honestly sam_builds · edited
Sending this link to half my friends nightshiftbuilder · edited
wes_codes · edited

python -m pip is the whole answer. Everything else is detail.

Comment
ingrid_h · edited

Cursor-specific: once the interpreter is selected, new terminals should activate the venv automatically (python.terminal.activateEnvironment setting). If your prompt doesn't show (.venv), that's why python still points somewhere else.

Comment