QuizCure
Find Available Solution Here!

modulenotfounderror no module named exceptions

Deepak May 28, 2024

Most of the exceptions are self-explanatory for us to understand the root cause. Here modulenotfounderror no module named exceptions notifying us that the imported module is not found during script execution.

But, As we include certain modules for the purpose which caused the error no module named exceptions. Let's understand the possible root case by dividing the current topic into the following sub-topics with demonstration code.

Typing mistake (Typo) in imported module name

There can be human typing error while writing module/package names to be imported in python script. Let's understand with following example code

#base.py
def introduce():
	return "I belongs to same Directory as of script test.py"
  
#test.py
import base

result = base.introduce()
print(result)
  

Output: I belongs to same Directory as of script test.py.

Well, All good.

But if we wrongly typed module name (basee rather than base). Please refer below program.

#test.py
import basee

result = base.introduce()
print(result)
 

It will raise error as ImportError: No module named basee Therefore the first step against solving no module named exceptions is to check spelling mistakes.

Wrong package path that interpreter couldn't found

This is the most common problem we face during development with directory structure. We place user-defined modules as per our needs. For example, for employee listing functionality we might have a number of modules such as departments module, finance module, hr modules, and more.

Therefore it is required to ensure the correct path of modules used in specific programs. Let's understand with following program.

We have the following files with their directory level

  • test.py
  • base.py
  • misc/inner.py

test.py & base.py are in the same directory whereas inner.py is in the misc folder.

#base.py
def introduce():
	return "I belong to the same Directory as of script test.py"
 
#misc/inner.py

def introduce():
	return "I'm in the subdirectory"

Running following code defined in test.py by importing base here

#test.py
import base
result = base.introduce()
print(result)
   

Output: I belong to the same Directory as script test.py

What if we try to import the inner module here instead of the base, let's try

#test.py
import inner
result = inner.introduce()
print(result)
  

ImportError: No module named inner

Explanation: Raised error as inner.py is not in the same directory as test.py. The interpreter couldn't find the imported package inner and complained.

Now we have a question: How can we import other directory modules?

Well, There is a way to access modules from another folder.

sys.path.append

Let the interpreter be aware of the package. We can use Sys.path.append to include the path of the specified module. Let's explore the following code example for demonstrations

import sys
sys.path.append('path_to_misc') 
# let's say path_to_misc : /home/admin/python/project/misc

import inner
result = inner.introduce()
print(result)
 

Output: I'm in sub directory

Explanation: sys.path is used to help the interpreter to search for the specified module. Therefore the inner module is searchable after setting the path for the misc folder using the append function.

Create an empty file __init__.py file in the directory

As we have an inner script in a misc folder we will create an empty file __init__.py in the misc directory. Therefore file hierarchy will be as below.

  • test.py
  • base.py
  • misc/inner.py
  • misc/__init__.py

File __init__.py in the misc directory is used to tell Python that misc is a package. Now we can rewrite the program as below.

from misc import inner

result = inner.introduce()
print(result)
   

Output: I'm in sub directory

Imported non installed external packages

Some of the external packages don't come with Python installation. Therefore it raises ImportError: No module named error if we import non-installed modules.

For example :

import requests
x = requests.get('https://www.quizcure.com')
print(x.status_code)
 

Will raise ImportError: No module named requests . because package requests is not installed my server

So what to do?

Install the request module and we are good to go :)

pip install requests

Some of the similar issues with the same reason are

python-docx not working 'ModuleNotFound'

ModuleNotFoundError as No module named 'docx'. Possible reasons could be:

  • Wrong installed module path set for the Python interpreter
  • module is not installed.

We can install docx by running command

pip install python-docx

Was this post helpful?

Send Feedback

Connect With QuizCure


Follow Us and Stay tuned with upcoming blog posts and updates.

Contributed By

Deepak

Deepak

QC STAFF
51 Posts
  • PHP
  • JAVA
  • PYTHON
  • MYSQL
  • SEO

You May Like to Read

Scroll up