Summary
A ModuleNotFoundError in Thonny usually means the interpreter that runs your script is different from the one where you installed the package. This mismatch is common when using Thonny’s built‑in virtual environment or when multiple Python versions are present on the system.
Root Cause
- Thonny was configured to use its bundled Python interpreter (or a virtualenv) while you installed the module into the system Python.
- The package was installed for a different user/site‑packages directory than the one Thonny accesses.
- On Windows/macOS, the PATH and PYTHONPATH differences between the terminals and Thonny cause the interpreter to search different locations.
Why This Happens in Real Systems
- Multiple interpreters: Modern dev machines often have Python 2, Python 3, Anaconda, and virtual environments side‑by‑side.
- Thonny’s sandbox: By default Thonny creates an isolated environment to protect the host system.
- IDE‑driven install: Installing via Thonny’s Tools → Manage packages sometimes targets the wrong interpreter if the interpreter was switched after the install.
Real-World Impact
- Runtime failures that appear only in the IDE, not in CI or on other machines.
- Wasted debugging time chasing import errors that are actually environment misconfigurations.
- Delayed releases when the missing‑module bug surfaces late in the development cycle.
Example or Code (if necessary and relevant)
# Verify which interpreter Thonny is using
python -c "import sys, pprint; pprint.pprint(sys.executable)"
# Install the package into that interpreter
/path/to/thonny/python -m pip install
How Senior Engineers Fix It
- Check the interpreter path shown at the bottom right of Thonny and confirm it matches the one used for
pip install. - Use Thonny’s “Run → Select interpreter” menu to switch to the interpreter where the package is installed, or reinstall the package using the interpreter shown in Thonny.
- Create a dedicated virtual environment and point Thonny to it:
python -m venv .venv- Activate and install required packages.
- In Thonny → Tools → Options → Interpreter → Alternative Python3 interpreter → select
.venv/bin/python.
- Validate the install inside Thonny’s REPL:
import sys, importlib.util print(sys.executable) print(importlib.util.find_spec('your_module')) - Add the interpreter’s site‑packages directory to
PYTHONPATHonly as a last resort.
Why Juniors Miss It
- They assume “installing a package” = “available everywhere” without checking which interpreter actually runs the code.
- They overlook Thonny’s virtual environment indicator and rely on the system terminal’s
pip. - They rarely verify the interpreter path during debugging, leading to repeated “module not found” errors.