| title | ModuleNotFoundError in Python trotz pip install | |||||
|---|---|---|---|---|---|---|
| domain | python | |||||
| tags |
|
|||||
| language | de | |||||
| status | published | |||||
| source | https://docs.python.org/3/tutorial/venv.html | |||||
| created | 2026-07-29 | |||||
| confidence | 0.9 | |||||
| verified_date | 2026-07-29 |
This error occurs when a Python module cannot be found at runtime even though pip install succeeded. Running a script fails with:
$ python script.py
Traceback (most recent call last):
File "script.py", line 1, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
But pip list shows the package as installed. This failure often happens after switching between projects or installing new packages.
The root cause is almost always a mismatch between the Python environment where pip install was run and the environment executing python script.py.
The three most common scenarios:
- Virtual environment not activated:
pip installran inside an activevenv, butpython script.pyruns outside it. - Multiple Python installations: The system has several Python versions (3.9, 3.10, 3.11).
pipinstalled into one,pythonuses another. pip install --uservs. system-wide: The package was installed with--userinto a local path, butPYTHONPATHdoes not include that path.
1. Identify which Python and pip are in use
which python
python --version
which pip
pip --versionEnsure python and pip point to the same version.
2. Use a virtual environment correctly
python -m venv .venv
source .venv/bin/activate # Linux/Mac
.venv\Scripts\activate # WindowsThen install and run:
pip install requests
python script.py3. With multiple Python versions, use the explicit interpreter
python3.11 -m pip install requests
python3.11 script.pyOr use pip3:
pip3 install requests
python3 script.py4. Check PYTHONPATH
python -c "import sys; print('\n'.join(sys.path))"Confirm that the site-packages path for your Python installation appears in the list.
5. Install with --target as a last resort
python -m pip install --target=$HOME/.local/lib/python3.11/site-packages requests
export PYTHONPATH=$HOME/.local/lib/python3.11/site-packages:$PYTHONPATH- Run
which pythonandwhich pip— both should point to the same installation - Execute
python -c "import requests; print(requests.__version__)"without errors - Inside a virtual environment, confirm
pip listshows the package - Test with a full script that uses the imported package
- Always create a dedicated virtual environment per project with
python -m venv - Do not commit
.venv/to your repository (add it to.gitignore) - On Windows, use
py -3.11instead ofpython3.11 - Re-run
pip install -r requirements.txtwheneverrequirements.txtchanges - Reference: Python venv documentation