Fix ModuleNotFoundError: No module named after pip install
pip install worked but you get ModuleNotFoundError: No module named? pip and python point at different environments. Compare two paths and use python -m pip.
Traceback (most recent call last): File "/srv/app/fetch.py", line 2, in <module> import requestsModuleNotFoundError: No module named 'requests'If pip install requests succeeded and you still get this error, the package was installed, just not into the Python that runs your script. The pip command and the python command point at different interpreters. Installing with python -m pip install from that same interpreter fixes it.
[1] Compare the path of the interpreter that failed with the path of pip. Below, pip install -q requests ran before the venv was activated, and the script ran after, producing the error above.
$ pip -V$ . .venv/bin/activate$ pip -V$ python -c "import sys; print(sys.executable)"pip 25.0.1 from /usr/local/lib/python3.12/site-packages/pip (python 3.12)pip 25.0.1 from /srv/app/.venv/lib/python3.12/site-packages/pip (python 3.12)/srv/app/.venv/bin/pythonThe install went to /usr/local (first line), while the script ran under /srv/app/.venv/bin/python (third line). If the leading part of the two paths (which folder they live under) differs, this is your case. The python 3.12 in parentheses only tells you the version; two environments can both be 3.12.
[2] Check whether the interpreter you run actually has the package.
$ python -m pip show requestsWARNING: Package(s) not found: requestspython -m pip runs pip with the python in front of it. The pip docs put it this way: /usr/bin/python3.7 -m pip runs pip for the interpreter at that path. That makes it clear where packages go, which bare pip does not.
[3] Install again with the same interpreter and run.
$ python -m pip install -q requests$ python fetch.py2.34.2Keep using python -m pip install … from now on. The stray copy installed under /usr/local at the start is still there, but leave it alone by default: other scripts or services may be using it, and removing it can make them stop with the same ModuleNotFoundError. If you must remove it, deactivate the venv, note which interpreter it is with python3 -m pip -V and the version (Version:) with python3 -m pip show requests, make sure nothing depends on it, then run python3 -m pip uninstall requests. To undo, reinstall the noted version with python3 -m pip install requests==<version> (this removal and restore were not tested). On systems where the distribution manages the system Python, such as Ubuntu 24.04, that install is blocked with externally-managed-environment in the first place; see Ubuntu 24.04 pip externally-managed-environment.
[4] If the package is in the venv but the script fails only under cron, systemd or ./script, check the shebang (first line). The run below is a separate test, not a continuation of the shell from [1]–[3]: a venv was created and requests installed only inside it with .venv/bin/python -m pip install requests, then the script was run from a shell where the venv is not activated (the default Python has no requests). In a shell with the venv activated, the shebang also picks the venv's Python and this failure does not happen.
$ ./fetch.pyTraceback (most recent call last): File "/srv/app/./fetch.py", line 2, in <module> import requestsModuleNotFoundError: No module named 'requests'$ head -1 fetch.py; command -v python3#!/usr/bin/env python3/usr/local/bin/python3#!/usr/bin/env python3 uses the first python3 on PATH. In a shell without the venv activated, in cron or in a service, that is the system Python. The Python docs say you do not need to activate a venv if you call its interpreter by full path. Fix it one of two ways:
| Method | Where |
|---|---|
/srv/app/.venv/bin/python fetch.py | cron lines, systemd ExecStart= |
first line #!/srv/app/.venv/bin/python | running the script directly |
Both printed 2.34.2 in the test. If you move or recreate the venv at another path, update this path too.
[5] Check whether the install name differs from the import name. This one happens even when the environment is right.
$ python -c "import pyyaml"Traceback (most recent call last): File "<string>", line 1, in <module>ModuleNotFoundError: No module named 'pyyaml'A package installed with pip install PyYAML is imported as yaml. To find the import name, look for folders with an __init__.py in the installed file list.
$ python -m pip show -f PyYAML | grep __init__ _yaml/__init__.py _yaml/__pycache__/__init__.cpython-312.pyc yaml/__init__.py yaml/__pycache__/__init__.cpython-312.pycIf it only fails in an IDE or Jupyter, print the interpreter that tool uses with sys.executable from [1]. If it differs from the python in your terminal, point the tool's interpreter setting at the venv's bin/python.