Last modified: December 17, 2020

How To Solve ModuleNotFoundError: No module named in Python

In this article, I am going to show you what are the reasons of this error and how to solve it.

1. The name of the module is incorrect

The first reason of this error is the name of the module is incorrect, so you have to check out the module name that you had imported.


For example, let's try to import 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 No module named 'oss'.


2. The path of the module is incorrect

The Second reason is Probably you would want to import a module file, but this module is not in the same directory.


Project structure


core.py
folder_1
---module.py

now, we want to import module.py

core.py


import module.py #incorrect

output:

ModuleNotFoundError: No module named 'module'

core.py


import folder_1.module.py #correct

output:


...Program finished with exit code 0

as you can see the problem has been solved


3. The Library not installed

Also, you can get the issue if you are trying to import a module of a library which not installed in your virtual environment. So before importing a library's module, you need to install it with the pip command.

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


installing:

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 library, the problem is solved.