Last modified: Sep 04, 2023 By Alexander Williams

How To Solve ModuleNotFoundError: No module named in Python

The ModuleNotFoundError: No module named error occurs when Python cannot find the module you are trying to import. This can happen for a few reasons:

  • Incorrect Module Name
  • Incorrect Module Path
  • File Extension Mismatch
  • Missing Library Installation
  • Unsupported Module
  • Python 2 vs. Python 3
  • Pip Version Mismatch
  • Incorrect PYTHONPATH

In this article, We'll discuss the reasons and the solutions for the ModuleNotFoundError error.

1. Incorrect Module Name:

One of the most common reasons for the "ModuleNotFoundError" is an incorrect module name.

For example, attempting to import the "os" module with a misspelled name like "oss" will result in an error:

>>> import oss
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'oss'

To resolve this, ensure that you use the correct module name:

>>> import os
>>> 

2. Incorrect Module Path:

The second reason for this error is an incorrect module path. You'll encounter the error if you're trying to import a module that's not in the same directory or not in the PYTHONPATH. For example:

Project structure:

core.py
folder_1
---my_module.py

 

import my_module # Bad
import folder_1.my_module # Good

3. File Extension Mismatch:

If your module's file extension differs from what Python expects (like.py.bin instead of.py), it may not be recognized as a proper module by Python. Make sure the file extension is .py.

Here is the structure:

project_directory/
    ├── my_module.txt
    └── main.py

Now, let's say you want to import and use my_module in your main.py script:

# main.py

import my_module

result = my_module.some_function()

print(result)

When you run main.py, you'll get an "ImportError: No module named 'my_module'" error because Python wants the module to have an a.py extension, not an a.txt extension.

4. Missing Library Installation:

If you attempt to import a library module not installed in your Python environment, you'll get the "ModuleNotFoundError." Install the necessary libraries using a package manager like "pip." For example:

>>> from bs4 import BeautifulSoup
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'bs4'

Now, let's install the library and try to re-import it:

pip install beautifulsoup4
Collecting beautifulsoup4
  Using cached https://files.pythonhosted.org/packages/d1/41/e6495bd7d3781cee623ce23ea6ac73282a373088fcd0ddc809a047b18eae/beautifulsoup4-4.9.3-py3-none-any.whl
Requirement already satisfied: soupsieve>1.2; python_version >= "3.0" in /home/py/Desktop/seo_pro/seo_env/lib/python3.6/site-packages (from beautifulsoup4) (1.9.5)
Installing collected packages: beautifulsoup4
Successfully installed beautifulsoup4-4.9.3

Re-importing:

>>> from bs4 import BeautifulSoup
>>> 

As you can see, after installing the package, the program is working.

5. Unsupported Module:

Some Python libraries drop support for specific modules in new updates. You'll encounter this error if you try to import an unsupported module. Always check the library's documentation to see if the module is still available.

6. Python 2 vs. Python 3:

As Python 2 is no longer supported, some libraries are exclusive to Python 3. If you're using Python 2, you may encounter this error when using modules designed for Python 3. Ensure you're using the correct Python version.

To check your Python version:

python -V
# Python 2.7.18

python3 -V
# Python 3.9.5

In my case, "python3" is the function that lets me use Python 3. So, I'll run the program with "python3." If Python3 is not installed on your computer, you should do so on Windows, Mac, or Linux.

7. Pip Version Mismatch:

Even though the mistake is easy, it happens a lot. You used pip3 install to install a Python library, which means it works with Python 3.x.

The solution is straightforward: always use the correct Python interpreter that matches the library's version.

For example, If you installed the libraries using pip3, run your program using the python3 interpreter. On the other hand, if you used pip to install the libraries, run your program with the standard Python interpreter.

8. Check PYTHONPATH:

Check that the PYTHONPATH environment variable is properly set to include the paths to the directories that hold your Python modules. This ensures that when you attempt to import your modules, Python can find them.

Conclusion

In conclusion, when facing the "ModuleNotFoundError: No module named" error:

  • Verify the module name.
  • Ensure the correct module path.
  • Check File Extension
  • Install the required libraries.
  • Check if the library supports the module.
  • Check PYTHONPATH
  • Check Pip Version Mismatch
  • Use Python 3 if Python 2 is incompatible.

These steps can resolve the error and ensure your Python code runs smoothly. We hope this article has helped you address this issue effectively.