ModuleNotFoundError looks like one problem but is four different ones wearing the same coat. Work through these checks in order and you will find yours within minutes.
Step by Step
- Read the error fully: it names the exact module Python could not find — note its spelling before changing anything.
- Install the module with the same Python that runs your script: use 'python -m pip install <module>' rather than a bare 'pip install', which may target a different interpreter.
- Check for a naming clash: if your own file is named like the library (for example 'random.py' or 'requests.py'), rename it — Python imports your file instead of the library.
- If you use a virtual environment, activate it in the same terminal before running, and confirm with 'python -c "import sys; print(sys.executable)"'.
- Print 'sys.path' from inside the script to see the folders Python actually searches when in doubt.
- Restart your editor after installing — many editors cache the interpreter's package list until restarted.
Nine times out of ten the cause is a mismatch between the Python that installed the package and the Python that runs the code. The 'python -m pip' habit prevents almost every recurrence.
Enjoying TCBuzz? Get the Briefing.
The five most important tech stories, one tutorial and one fact — in your inbox every weekday morning, free.
You're on the list — please check your inbox to confirm your subscription.
No spam · Unsubscribe in one click · See our Privacy Policy