ModuleNotFoundError
Python Programming Language
Severity: ModerateWhat Does This Error Mean?
A ModuleNotFoundError means Python cannot find the module (library or package) you are trying to import. Either the module is not installed, it is installed for a different Python version, or there is a typo in the module name. This is one of the most common errors for beginners learning to use third-party Python libraries.
Affected Models
- Python 3.6 and later
Common Causes
- The module is not installed — it needs to be added with pip install
- The module is installed for a different Python version or a different virtual environment
- A typo in the module name in the import statement
- You created a file with the same name as the module you are trying to import
- The module is installed but in a different location than the Python interpreter is looking
How to Fix It
-
Install the missing module using pip. Open your terminal and run: pip install module_name (replace module_name with the actual module name from the error).
Example: if you get ModuleNotFoundError: No module named 'requests', run: pip install requests
-
If pip install did not help, make sure you are using the right pip for your Python version. Try: python -m pip install module_name — this ensures you install into the exact Python you are running.
On some systems, 'python' runs Python 2 and 'python3' runs Python 3. Use 'python3 -m pip install module_name' if needed.
-
Check for a typo in the import statement. Module names must be spelled exactly right. Common example: it is 'import PIL' not 'import Pillow' (even though the package is installed with 'pip install Pillow').
Some packages have a different import name than their pip install name. Check the package's documentation.
-
If you are using a virtual environment, make sure it is activated. Packages installed in one virtual environment are not available in another.
Activate with: source venv/bin/activate (Mac/Linux) or venv\Scripts\activate (Windows)
-
Check if you have a file in the same folder with the same name as the module. If you have a file called 'requests.py', Python finds it instead of the real requests library.
Rename your file to something that does not clash with any module name.
When to Call a Professional
ModuleNotFoundErrors are always something you can fix yourself. Installing packages with pip is the most common solution. If you are using virtual environments, make sure you have activated the right one before installing.
Frequently Asked Questions
How do I know which pip install command to use for a module?
The import name and the install name are not always the same. For example, you install it with 'pip install Pillow' but import it with 'import PIL'. Always check the official documentation or the package's page on pypi.org to find the correct install command.
I installed the module but still get ModuleNotFoundError — why?
Most likely it was installed for a different Python. Run 'python -m pip install module_name' to make absolutely sure it installs into the right place. Also check if you are inside a virtual environment that does not have the package.
What is a virtual environment and why does it affect module installation?
A virtual environment is an isolated Python setup — think of it as a separate room with its own set of installed packages. Packages installed outside the virtual environment are not visible inside it, and vice versa. If you use virtual environments, you must install packages inside each one that needs them.