Python + AI/ML · Tutorial 12 of 12
Fix Python ModuleNotFoundError and Virtual Environment Problems
By the FUTEAC Team · 25 November 2026 · 13 min read · Beginner
In short
ModuleNotFoundError means the Python that ran your code looked for a module and did not find it. Almost every case has one of three causes: the package was never installed, it was installed for a different Python, or the code runs from the wrong folder. This tutorial finds which cause you have, then fixes it.
- The error names a module, not a bug in your code.
ModuleNotFoundError: No module named 'requests'says Python could not findrequests. Your logic is probably fine. - There is rarely one Python on a computer. The
pythonin your terminal, the one inside a virtual environment, and the one your editor uses can be three different programs with three different sets of installed packages. - A virtual environment is one folder per project with its own Python and its own packages. Make one for every project, and this whole class of error mostly disappears.
- Always install with
python -m pip install, never barepip install. The-mties the install to the exact Python you mean. - This tutorial feeds the Python Programming Foundations course (/course/python-programming-foundations) and the Python Programming skill (/skill/python).
Before you start
What the error is telling you
Make a practice folder and a fresh virtual environment. Run these commands in your terminal:
mkdir /tmp/moderr
cd /tmp/moderr
python3 -m venv .venvNow write a two-line file called try_requests.py:
import requests
print("requests", requests.__version__)Run it with the environment's Python:
.venv/bin/python try_requests.pyYou get:
Traceback (most recent call last):
File "/tmp/moderr/try_requests.py", line 1, in <module>
import requests
ModuleNotFoundError: No module named 'requests'Read it as a sentence: "I looked for a module called requests and it is not in any place I look." The last line is the whole diagnosis. The lines above it are just the route the program took to get there. Beginners read the traceback as one big error. It is not. The last line is the error, and the rest is the address.
Python looks in a short list of places: the folder the script lives in, then the standard library, then the site-packages folder of the Python that is running. A fresh virtual environment has an empty site-packages apart from pip itself, which is why the error appears. Check that with:
.venv/bin/python -m pip listYou should see:
Package Version
------- -------
pip 24.0Nothing else is installed. That is the point of a virtual environment: it starts empty, so you always know what is in it. The fix is to put requests into this Python's site-packages, which is section 4. But first, section 2, because installing into the wrong Python is the mistake that makes this error come back.
First, find out which Python ran your code
Think of it like posting a letter to the wrong house. If two houses share a street name, the letter arrives at the wrong door and the person at the right door never sees it. pip install and python are the two houses. If they belong to different Pythons, the package lands where your code never looks.
Ask the Python that runs your code where it lives:
.venv/bin/python -c "import sys; print(sys.executable)"It prints:
/tmp/moderr/.venv/bin/pythonThat path is the answer to "which Python". Now ask the same question of the plain python3 on your machine:
python3 -c "import sys; print(sys.executable)"On the machine used for this tutorial, that prints /usr/bin/python3. Two different paths, two different Pythons, two different site-packages folders. A package installed for one is invisible to the other. That is the whole trap, and it is also why the next section matters: once you are inside a virtual environment, there is only one Python in play, and the trap closes.
Two commands to keep in your pocket. which python shows which program your terminal would run. python -c "import sys; print(sys.executable)" shows which program actually ran, which is the one that counts. When an import fails, run the second one first.
Make one virtual environment per project
A virtual environment is a folder with its own copy of Python and its own site-packages. One per project means each project's packages never touch another project's. Your expense tracker can use one version of a library while your machine learning project uses another, and neither knows the other exists.
Create it inside your project folder, once per project:
python3 -m venv .venvThe name .venv is a convention, not a rule. The leading dot hides the folder from normal listings, which is what you want: you will never edit anything inside it by hand.
Now switch the terminal into it. On macOS and Linux:
source .venv/bin/activateOn Windows, in PowerShell:
.\.venv\Scripts\activateThe prompt changes to show (.venv) at the start. That prefix is your proof that the environment is active. Check it:
which python
which pipOn the machine used for this tutorial, with the environment active, those printed:
/tmp/moderr/.venv/bin/python
/tmp/moderr/.venv/bin/pipBoth point inside .venv. Now python and pip belong to the same Python, and the letter goes to the right house. Deactivate when you are done with deactivate, and check again: which python3 goes back to /usr/bin/python3.
One rule for later: never install packages into your system Python. The system Python belongs to your operating system, and other tools depend on it. The virtual environment is yours. Keep the system one clean.
Install the package into the right Python
With the environment active, install with python -m pip install, not bare pip:
python -m pip install requestsYou should see:
Installing collected packages: urllib3, idna, charset_normalizer, certifi, requests
Successfully installed certifi-2026.11.20 charset_normalizer-3.5.3 idna-3.20 requests-2.34.2 urllib3-2.8.0requests brings four small libraries with it, which is normal. Now run the file again:
python try_requests.pyIt prints:
requests 2.34.2The same two lines that failed before now run. Nothing about the code changed. Only the environment changed, which is the lesson: when ModuleNotFoundError appears for a library, the code is usually innocent and the environment is guilty.
Confirm what is installed:
python -m pip listIt now shows requests 2.34.2 alongside pip. Get into the habit of running pip list when an import fails. If the package is absent from the list, it was never installed for this Python. If it is present and the import still fails, you are running a different Python, which is section 6.
Why python -m pip instead of pip? Because pip on its own is a separate program, and it can belong to a different Python than the python you just typed. python -m pip says "use the pip module inside this exact Python", so the install and the code can never drift apart. It costs four extra characters and removes a whole category of confusion.
When the missing module is your own file
Not every ModuleNotFoundError is about a library from the internet. Sometimes the missing module is a file you wrote yourself. The cause is different, and so is the fix.
Set up a small project:
myproject2/
helpers.py
scripts/
run.pyhelpers.py holds one function:
def double(n):
return n * 2And scripts/run.py imports it:
from helpers import double
print(double(21))Run it from the project folder, the way a beginner naturally would:
python scripts/run.pyYou get:
Traceback (most recent call last):
File "/tmp/moderr/myproject2/scripts/run.py", line 1, in <module>
from helpers import double
ModuleNotFoundError: No module named 'helpers'The file is right there in the project folder. Why can Python not see it? Because Python puts the folder the *script* lives in at the front of its search path, not the folder you ran the command from. The script lives in scripts/, so Python looks in scripts/, and helpers.py is one folder up, in the project root. It never looks there.
The fix that keeps the project tidy is to run the script as a module, from the project folder:
python -m scripts.runIt prints:
42The -m flag tells Python to run scripts.run as part of the project, so the project folder joins the search path and helpers is found. No files move, no code changes. The only difference is how you launch it.
Two other fixes exist, and both are worth knowing. You can move run.py up into the project folder next to helpers.py, so the script folder and the module folder are the same. Or you can turn scripts into a package by adding an empty __init__.py file, though the -m run works without one in Python 3.3 and later. Our rule: run project scripts with python -m from the project root. It is the one habit that stops this error from ever appearing for your own files.
The wrong turn: installing into one Python and running another
Here is the wrong turn this tutorial exists to prevent, reproduced live so you can see it fail. Two virtual environments were made side by side, .venv and .venv2. requests was installed into the first one only. The first one runs the file fine:
2.34.2The second one runs the same file, in the same folder, a second later:
Traceback (most recent call last):
File "/tmp/moderr/try_requests.py", line 1, in <module>
import requests
ModuleNotFoundError: No module named 'requests'Same file, same folder, same command shape. The only difference is which Python ran it:
/tmp/moderr/.venv/bin/python
/tmp/moderr/.venv2/bin/pythonEach environment has its own site-packages, and requests lives in the first one only. The beginner's version of this mistake is installing with the system pip while the editor runs the code with a virtual environment's Python, or the other way round. The error message gives no hint about it, because as far as Python is concerned nothing is wrong: the module genuinely is not there, in the Python that ran.
The way out is always the same two checks. First, python -c "import sys; print(sys.executable)" to see which Python ran. Second, python -m pip list to see what that Python has installed. If the package is missing from that list, install it with python -m pip install using that same Python. If you installed it somewhere and the error persists, you installed it into a different Python. That is not a guess. It is the only remaining explanation.
How to verify the fix worked
Run these four checks in order, in the terminal, inside your project folder with the environment active:
python -c "import sys; print(sys.executable)"
python -m pip list
python try_requests.py
python -m scripts.runYou should see the environment's Python path, requests 2.34.2 in the package list, requests 2.34.2 printed by the first script, and 42 printed by the second. Four green checks, four different failure modes ruled out: the wrong Python, the missing package, the broken import, and the wrong launch folder.
If any check fails, stop there and fix that step before moving on. A passing pip list with a failing import means a different Python is running your code. A passing import with a failing python -m scripts.run means the launch folder is wrong. Each check points at exactly one cause, which is what makes this a ten-minute fix instead of an afternoon.
Try it yourself
1. Make a new folder, create a fresh virtual environment in it, and run a file that imports requests before installing anything. Read the traceback, then install with python -m pip install requests and run it again. Watch the same file go from error to requests 2.34.2. 2. In that same folder, run python -c "import sys; print(sys.executable)" before and after source .venv/bin/activate. Write down the two paths. They are the two Pythons the whole tutorial is about. 3. Build the myproject2 example above: helpers.py in the project folder, run.py in a scripts subfolder. Run it both ways, python scripts/run.py and python -m scripts.run, and confirm the first fails and the second prints 42. 4. Open the Python Programming Foundations course and find the lesson "Installing Python and Writing Your First Program". Add a virtual environment step to your own notes for that lesson, so the next project starts inside one.
Continue in the free course: Python Programming Foundations →
Common mistakes
- Installing with bare
pipwhile a different Python runs the code.pip install requestscan belong to the system Python while your editor runs the virtual environment's Python. Usepython -m pip installevery time, with the same Python that runs the code. - Installing packages into the system Python. It works once, then breaks something else later, and you cannot tell which project needed which package. One virtual environment per project, and the system Python stays clean.
- Running a script from the wrong folder and blaming the import.
python scripts/run.pyfails onfrom helpers import doublebecause Python searches the script's folder, not your folder. Run it as a module withpython -m scripts.runfrom the project root. - Reading the whole traceback instead of the last line. The last line names the missing module. The lines above it are the route, not the error. Start at the bottom.
- Reinstalling the package three times instead of checking which Python ran. If
pip listshows the package and the import still fails, installing again changes nothing. Checksys.executablefirst. - Forgetting to activate the environment in a new terminal window. Each terminal window starts outside the environment. If the import worked yesterday and fails today, the first thing to check is whether
(.venv)is in your prompt.
Frequently asked questions
Is ModuleNotFoundError the same as ImportError?
Almost. ModuleNotFoundError is a more specific kind of ImportError, added in Python 3.6, for the case where the module itself cannot be found. If you see ImportError instead, the module was found but something inside it failed to import, which is a different problem.
Do I need a virtual environment for every tiny script?
For a one-file experiment, no. For anything you will open again tomorrow, yes. It takes one command to make, and it is the difference between "it worked on my machine" and "it works".
What is the difference between pip and pip3?
On most machines they are the same tool under two names, and both can belong to a different Python than the one running your code. That is why this tutorial uses python -m pip: it removes the doubt entirely.
Should I add .venv to version control?
No. Add .venv/ to your .gitignore file. The environment is rebuilt from your requirements, not shared. What you share is the list of packages, usually in a requirements.txt file made with python -m pip freeze > requirements.txt.
Why did the error appear after I installed the package yesterday?
Because yesterday's terminal had the environment active and today's does not, or yesterday you installed into one Python and today another one is running. Run python -c "import sys; print(sys.executable)" and python -m pip list and the answer will be in front of you.
Follow the Python + AI/ML Beginner Roadmap
Learn Python from zero and take your first steps into AI and machine learning. Free, step by step, at your pace.
View the Roadmap