Last modified: Jan 10, 2023 By Alexander Williams

How To Solve ModuleNotFoundError: No module named in Python

ModuleNotFoundError: No module named in Python occurs when:

  • The name of the module is incorrect
  • The path of the module is incorrect
  • The Library is not installed
  • The module is unsupported
  • Python 2 instead of Python 3

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

1. The name of the module is incorrect

The first reason for ModuleNotFoundError: No module named is the module name is incorrect. For example, let's try to import the os module with double "s" and see what will happen:

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

As you can see, we got ModuleNotFoundError: No module named 'oss.' To solve the error, make sure that you use the correct module name.

Let's import the module with the correct name.

>>> import os
>>> 

As you can see, the error is solved.

2. The path of the module is incorrect

The Second reason is the path of the local module you want to import is incorrect. for example, let's see a directory structure 

Project structure:


core.py
folder_1
---my_module.py

In this folder, we've:

  • core.py: is the file that will execute
  • folder_1: folder contains my_module.py

Now in core.py, let's try to import my_module.py

core.py


import my_module #incorrect

Output:

ModuleNotFoundError: No module named 'my_module'

As you can see, we got the error because my_module.py is not in the path that we've executed core.py. We need to define the module's path in the following example to solve the error.

core.py


import folder_1.my_module #correct

Output:

...Program finished with exit code 0

Now we've imported m_module successfully.

3. The library is not installed

Also, you can get the ModuleNotFoundError: No module named issue if you are trying to import a library module that is not installed in your virtual environment or computer.

So before importing a library's module, you need to install it with any package-management system.

For example, let's try to import the Beautifulsoup4 library that's not installed in my virtual environment.

>>> 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.

4. The module is unsupported

When a library releases a new update, new modules are added, and others are dropped to support it.

If you try to import a module that is n unsupported by the library, you will get ModuleNotFoundError: No module named.

To ensure the module is supported, go to the package documentation and check if the module is available or not.

5. Python 2 instead of Python 3

As you know, Some Python libraries no longer support Python 2. For that reason, you'll get the ModuleNotFoundError error if you execute a module that does not support Python 2 with Python 2.

To solve the error:

First, let's check our python version using these two commands:

python -V
# Python 2.7.18

python3 -V
# Python 3.9.5

In my case, Python 3 is on the python3 command, So I will execute the program using python3. If Python3 is not found on your device, Install Python on Windows, Mac, and Linux.

Conclusion

In conclusion, To solve the  ModuleNotFoundError: No module named:

  1. Ensure the name of the module is incorrect
  2. Ensure the path of the module is incorrect
  3. Ensure the Library is installed
  4. Ensure the module is supported
  5. Ensure using Python 3

Finally, I hope your problem has been fixed.