How to fix ModuleNotFoundError: No module named '...'
ModuleNotFoundError: No module named '...'. The error means Python cannot find a package or file you try to use. Many new and even experienced coders see this error. It is a top question online right now because people move to new Python versions, use virtual environments, or share code across machines.Quick answer
If you see ModuleNotFoundError, first check you installed the package for the same Python you run. Use python -m pip install packagename (or py -m pip install packagename on Windows). If you use a virtual environment, activate it before you run your code. These checks fix most problems.
Step-by-step solution
-
Find the Python that runs your code.
Open a terminal and run:
This shows the exact Python program. If this is not the one you expect, later installs may go to a different place.python --version python -c "import sys; print(sys.executable)" -
Install with that Python.
Run:
Usingpython -m pip install packagename-m pipmakes pip run inside that same Python. This avoids mixing system pip and a different Python. -
Use a virtual environment for each project.
Make a clean venv:
Then install packages while the venv is active. This keeps project packages separate and reduces conflicts.python -m venv venv source venv/bin/activate # macOS/Linux venv\Scripts\activate # Windows -
Check your file and folder names.
If your file is namedrequests.pyor a folder is namedjson, Python may import that file instead of the real package. Rename your file and remove any__pycache__folders, then try again. -
Print
sys.pathto see import paths.
In a short script:
Python looks in these places for modules. If your code or package folder is not listed, Python cannot import it.import sys print(sys.executable) for p in sys.path: print(p) -
Reinstall or update the package.
Sometimes an install was partial. Run:
On some systems usepython -m pip uninstall packagename python -m pip install packagename--useror use a venv to avoid admin rights issues. -
Check version and system requirements.
Some packages need a certain Python version or extra system libraries (likelibxml2). Read the package page on PyPI or its docs and install any needed OS libraries, then reinstall the Python package. -
Try a fresh environment if all else fails.
Make a new virtual env and only add the packages you need. This clears hidden conflicts from many installs and shows if the problem is in your project or your machine.
Example common cases (simple)
- Installed but import fails: You did
pip install requestsbutimport requestsfails. Fix: runpython -m pip install requestsfor the Python you use, or activate your venv first. - File name clash: Your script is
myapp/requests.pyandimport requestsfails. Fix: renamerequests.pytomy_requests.pyand delete__pycache__. - Windows with both
pyandpython: Usepy -m pip install packagenameto be safe.
Troubleshooting and extra tips
- Use
python -m pip --versionto check which pip belongs to which Python. - If your IDE runs a different interpreter, change the IDE setting to the right Python (PyCharm, VS Code let you pick the interpreter).
- Clear caches: remove
__pycache__folders and restart your Python shell or IDE. - If you build packages from source, use wheels when possible. Wheels install faster and fail less than building from source.
- On Linux, avoid
sudo pip install. Use distro packages,--user, or a venv. System-wide pip installs can break the system Python.
Useful Sources
Read more at the official docs and helpful guides:
Who this helps
This guide helps learners, students, and developers who get stuck when code moves to a new machine or when they try a new Python version.
FAQ
Is ModuleNotFoundError the same as ImportError?
Answer: ModuleNotFoundError is a kind of ImportError. It means Python did not find the module you asked for.
Why can I import locally but not on a server?
Answer: The server may use a different Python or not have the package installed. Repeat the install steps on the server and use a venv.
Can wrong file names cause this error?
Answer: Yes. A local file that shares a package name can hide the real package. Rename it and clear caches.
I followed all steps. What now?
Answer: Copy sys.executable and sys.path output and ask on a help site or include them in an issue. That information helps others find the cause.
More tips and tools (easy)
- See installed packages:
This shows the exact package names and versions.python -m pip list - Find where a package is installed:
Look for Location: in the output. That folder should be onpython -m pip show requestssys.path. - Editable installs for local packages:
This makes your package importable while you edit its files.python -m pip install -e . - Conda users: If you use conda, install with
conda install packagenameor usepipinside the conda env. Make sure the conda env is active. - Windows common fix: Use
py -m pip install packagenameto pick the right Python. Usewhere pythonto see which Python is on your PATH. - When a package uses native code: Some packages need compiled extensions (Visual C++ build tools on Windows,
build-essentialon Linux). If wheel build fails, read the package docs for system steps.
Real world example (step-through)
- I run
python3 main.pyand seeModuleNotFoundError: No module named 'foo'. - I run
python3 -m pip listandfoois not on the list. - I run
python3 -m pip install foo. Install finishes. - I run
python3 main.pyagain and nowimport fooworks.
This exact flow fixes many cases. If install fails, check the pip output for missing system packages. If install succeeds but import fails, check file names and sys.path.
Final checklist (short)
- Did you run
python -m pip install packagename? - Is your virtual env active?
- Are you using the same Python in your IDE and terminal?
- Is your script or folder name clashing with the package name?
- Did you restart your shell or IDE after changes?
If yes and it still fails, gather sys.executable, sys.path, and pip show packagename, then ask for help with that info.
Conclusion
ModuleNotFoundError is common and fixable. Most times you just need to install the package into the right Python or use a virtual env. If you follow the steps above, you will solve most cases. Keep notes on which Python and pip you use — it will save time later. Happy coding!

Comments
Post a Comment