How to Fix Python ModuleNotFoundError: No Module Named
Python's ModuleNotFoundError: No module named means the Python that ran your script searched its own list of folders and didn't find that name. On a real Ubuntu PC we fix the common causes: a script whose first line runs the system Python while pip installed into a virtual environment, a new terminal window where the environment isn't active, sudo running the system Python, an install name that isn't the import name (pip install pillow, import PIL), and your own module not found when you run a script from a subfolder (python -m).
Already installed, still not found
Your script stops with ModuleNotFoundError, yet pip says the package is already installed. John keeps his family's recipe site in Python. pip says PyYAML, the package his build script needs to import yaml, is already there. The script says: No module named yaml. So which one is wrong?

In his terminal, the prompt starts with.venv in brackets: his project's virtual environment is active. Installed, then not found. There are four usual reasons, one of them a script that can't find a folder sitting right next to it.
(.venv) john@family-pc:~/recipe-site$ pip install pyyamlRequirement already satisfied: pyyaml in ./.venv/lib/python3.12/site-packages (6.0.4)
(.venv) john@family-pc:~/recipe-site$ ./build.pyTraceback (most recent call last): File "/home/john/recipe-site/./build.py", line 6, in <module> import yaml ModuleNotFoundError: No module named 'yaml'
The script's first line picks another Python
The usual fix starts with one question: which Python ran the script? A computer can have several Pythons, each with its own packages. John ran his script as./build.py. When you run a file directly like this, without typing python first, Linux reads its first line to pick the program. head -1 prints that line, called the shebang: /usr/bin/python3, the system's Python. But pip put PyYAML in the.venv. And which python shows that plain python, in this window, is the one inside.venv. So run the script with python build.py instead, and all six recipe pages build.
(.venv) john@family-pc:~/recipe-site$ head -1 build.py#!/usr/bin/python3
(.venv) john@family-pc:~/recipe-site$ which python/home/john/recipe-site/.venv/bin/python
(.venv) john@family-pc:~/recipe-site$ python build.pyBuilt 6 recipe pages in public/
To fix the file itself, change that line to /usr/bin/env python3. PATH is the list of folders your shell searches for commands, and env runs the first python3 it finds there. John makes that change, and with the environment active,./build.py builds all six pages too.

Where import looks
The first line is only one way to end up in the wrong Python. To spot the others, you need to know where import looks. ModuleNotFoundError is Python saying: I searched every folder on my list, and that name isn't in any of them. The list goes in order. First, the script's own folder. Then Python's standard library. Then site-packages, where pip puts what it installs. The first match wins. And every Python has its own list. The environment's Python has PyYAML in its site-packages. The system Python never looks in that folder at all.

Think of a recipe box in each kitchen. A cook only looks in the box in the kitchen they're standing in. Put Grandma's lasagna card in your sister's box, then cook at home, and the card is nowhere to be found, even though it exists.

You can print the list. python -m site runs a small built-in tool that shows the folders, in order. The environment's list ends in its own site-packages. Ask the system Python, and its list ends in dist-packages, Ubuntu's name for the same idea. So when pip says installed and import says no, ask the Python itself: the one-liner on screen prints sys.executable, the path of the Python that's running. Then install with that exact Python: python -m pip install. That pip can only put packages where that Python looks.
(.venv) john@family-pc:~/recipe-site$ python -m site | head -7sys.path = [ '/home/john/recipe-site', '/usr/lib/python312.zip', '/usr/lib/python3.12', '/usr/lib/python3.12/lib-dynload', '/home/john/recipe-site/.venv/lib/python3.12/site-packages', ]
(.venv) john@family-pc:~/recipe-site$ /usr/bin/python3 -m site | head -8sys.path = [ '/home/john/recipe-site', '/usr/lib/python312.zip', '/usr/lib/python3.12', '/usr/lib/python3.12/lib-dynload', '/usr/local/lib/python3.12/dist-packages', '/usr/lib/python3/dist-packages', ]
(.venv) john@family-pc:~/recipe-site$ python -c 'import sys; print(sys.executable)'/home/john/recipe-site/.venv/bin/python
(.venv) john@family-pc:~/recipe-site$ python -m pip install pyyamlRequirement already satisfied: pyyaml in ./.venv/lib/python3.12/site-packages (6.0.4)
New window: environment not active
The most common way to hit this: a new terminal window. John opens a second one in the same folder and runs python3 build.py. No module named yaml again. Look at the prompt: no.venv in front of it, so the environment isn't active here, and python3 is the system's. There isn't even a pip command for it on stock Ubuntu. Two fixes: call the environment's Python by its path,.venv/bin/python, or activate the environment in this window first, with source.venv/bin/activate. Both build the site.
python3 build.pyTraceback (most recent call last): File "/home/john/recipe-site/build.py", line 6, in <module> import yaml ModuleNotFoundError: No module named 'yaml'
which python3/usr/bin/python3
pip install pyyaml-bash: pip: command not found
.venv/bin/python build.pyBuilt 6 recipe pages in public/
source .venv/bin/activate && python3 build.pyBuilt 6 recipe pages in public/
sudo's own PATH
Now John wants the pages in the web server's folder, which belongs to root, the system's administrator account. Permission denied, so he reaches for sudo, still in the window where the environment is active. sudo runs a command as root. Guess first: does sudo python3 find yaml? It doesn't. Same error.
(.venv) john@family-pc:~/recipe-site$ python build.py /var/www/recipesTraceback (most recent call last): File "/home/john/recipe-site/build.py", line 15, in <module> (out / f"{slug(recipe['name'])}.html").write_text(page(recipe)) File "/usr/lib/python3.12/pathlib.py", line 1049, in write_text with self.open(mode='w', encoding=encoding, errors=errors, newline=newline) as f: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.12/pathlib.py", line 1015, in open return io.open(self, mode, buffering, encoding, errors, newline) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PermissionError: [Errno 13] Permission denied: '/var/www/recipes/apple-crumble.html'
(.venv) john@family-pc:~/recipe-site$ sudo python3 build.py /var/www/recipesTraceback (most recent call last): File "/home/john/recipe-site/build.py", line 6, in <module> import yaml ModuleNotFoundError: No module named 'yaml'
Ask sudo which Python it ran: /usr/bin/python3. For safety, sudo swaps your PATH for its own short list of folders, called secure path. Your environment isn't on that list. It's cooking in another kitchen. So hand sudo the full path: sudo.venv/bin/python build.py, and the pages land in the web folder. Better still, give John's own account that folder, and skip sudo for Python altogether.
(.venv) john@family-pc:~/recipe-site$ sudo python3 -c 'import sys; print(sys.executable)'/usr/bin/python3
(.venv) john@family-pc:~/recipe-site$ sudo grep secure_path /etc/sudoersDefaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
(.venv) john@family-pc:~/recipe-site$ sudo .venv/bin/python build.py /var/www/recipesBuilt 6 recipe pages in /var/www/recipes/
PIL and pillow
What if the name itself is the problem? Jen, John's sister, sends him her script that makes a small photo for each recipe. It stops on No module named PIL. PIL is the name in her import line. Guess first: does pip install PIL fix it? No. pip can't find anything called PIL.
(.venv) john@family-pc:~/recipe-site$ python tools/thumbs.pyTraceback (most recent call last): File "/home/john/recipe-site/tools/thumbs.py", line 4, in <module> from PIL import Image ModuleNotFoundError: No module named 'PIL'
(.venv) john@family-pc:~/recipe-site$ pip install PILERROR: Could not find a version that satisfies the requirement PIL (from versions: none) ERROR: No matching distribution found for PIL
The name you install isn't always the name you import. The package is called Pillow; it keeps the old import name, PIL. PyYAML installs a module called yaml. pip install pillow works, and pip freeze, which lists what's installed, shows the install names. When an import fails, search the web for the import name plus the word pip, and install the name the project's own page gives.
(.venv) john@family-pc:~/recipe-site$ pip install pillowCollecting pillow Downloading pillow-13.0.0-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl.metadata (9.1 kB) Downloading pillow-13.0.0-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl (6.5 MB) Installing collected packages: pillow Successfully installed pillow-13.0.0
(.venv) john@family-pc:~/recipe-site$ pip freezepillow==13.0.0 PyYAML==6.0.4
A few common ones are on screen. For example, import cv2 comes from the package opencv-python, and bs4 from beautifulsoup4.

Your own module, run from a subfolder
Jen's script runs again, and stops one line later: No module named family. But family isn't from pip. It's John's own folder of helper code, and ls, which lists the project folder, shows it right there, next to the tools folder that holds Jen's script. So why can't Python see a folder that's right there?
(.venv) john@family-pc:~/recipe-site$ python tools/thumbs.pyTraceback (most recent call last): File "/home/john/recipe-site/tools/thumbs.py", line 6, in <module> from family.render import slug ModuleNotFoundError: No module named 'family'
(.venv) john@family-pc:~/recipe-site$ lsbuild.py family photos public recipes tools
Python's tutorial says why. The first folder on the search list is the directory containing the input script. Jen's script is in tools, so Python searches tools, not the project folder John is standing in.

The fix: from the project folder, type python -m tools.thumbs. That's a dot where the slash was, and no.py at the end. With -m, Python searches the folder you're in first, so it finds family, and all six thumbnails are made.
(.venv) john@family-pc:~/recipe-site$ python -m tools.thumbsMade 6 thumbnails in public/thumbs/
Keep it from coming back
On a new laptop, or after deleting.venv, every one of these imports would fail again, unless the project remembers its packages. pip freeze writes them down, by install name, into a file called requirements.txt. To prove the file works, John deletes his.venv and makes a fresh one, then installs from the list with the new environment's own pip, using dash r for the file. Then both scripts run. Calling the environment's Python by its full path works even when nothing is activated.
(.venv) john@family-pc:~/recipe-site$ python -m pip freeze > requirements.txt; cat requirements.txtpillow==13.0.0 PyYAML==6.0.4
(.venv) john@family-pc:~/recipe-site$ deactivate; rm -rf .venv; python3 -m venv .venv; .venv/bin/python -m pip install -r requirements.txtCollecting pillow==13.0.0 (from -r requirements.txt (line 1)) Using cached pillow-13.0.0-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl.metadata (9.1 kB) Collecting PyYAML==6.0.4 (from -r requirements.txt (line 2)) Using cached pyyaml-6.0.4-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.metadata (2.3 kB) Using cached pillow-13.0.0-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl (6.5 MB) Using cached pyyaml-6.0.4-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (814 kB) Installing collected packages: PyYAML, pillow Successfully installed PyYAML-6.0.4 pillow-13.0.0
.venv/bin/python build.py && .venv/bin/python -m tools.thumbsBuilt 6 recipe pages in public/ Made 6 thumbnails in public/thumbs/
Recap
So, when you see No module named: find which Python ran it, install with that Python's pip, check the install name, and run your own code from the project folder with python -m.

If this got your script running, subscribing brings more Python fixes, tried on a real machine.
Every command and every error in this video came from a real Ubuntu PC, and the lab files are linked in the description. Thanks for watching Cloud Explainers.
Sources
- Python tutorial, Modules: The Module Search Path
- Python documentation, The initialization of the sys.path module search path (python -m adds the current directory)
- Python documentation, sys.executable
- Python Packaging User Guide, Installing packages (python3 -m pip)
- sudoers manual, secure_path
- Pillow on PyPI (install pillow, import PIL)
- PyYAML on PyPI
Demos: one container on our own Mac standing in for a family PC; the recipe site and its scripts are made up. Every command, error and number was real. Watch the full video on YouTube.