QuizCure
Find Available Solution Here!

modulenotfounderror no module named exceptions

QuizCure Member Mar 19, 2023

Most of the exceptions are self-explanatory for us to understand the root cause. Here modulenotfounderror no module named exceptions notifying us that 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 demonstrations 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 following files with there directory level

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

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

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

def introduce():
	return "I'm in sub directory"

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

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

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

What if we try to import inner module here instead of 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. Interpreter couldn’t find the imported package inner and complained here.

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

Well, There is a ways 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 explore following code example for demonstrations

import sys
sys.path.append('path_to_misc') 
#let 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 interpreter to search for the specified module. Therefore the inner module is searchable after setting the path for misc folder using append function.

Create an empty file __init__.py file in the directory

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

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

File __init__.py in misc directory is used to tell python that misc is a package. Now we can rewrite 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 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

QuizCure Member
51 Posts
  • PHP
  • JAVA
  • PYTHON
  • MYSQL
  • SEO

You May Like to Read

Other Python Articles