Open In App

ModuleNotFoundError: No module named Error in Python

Last Updated : 05 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

One common error that developers often come across is the ModuleNotFoundError error. The ModuleNotFoundError error is raised when Python attempts to import a module that it cannot locate. In this article, we will see the reasons and approaches to solving ModuleNotFoundError in Python.

What is ModuleNotFoundError: No module named Error in Python?

The “No Module Named…” error is raised when Python attempts to import a module that it cannot locate. Modules are essentially Python files containing functions and variables that can be reused in different parts of a program. When Python encounters an import statement, it searches for the specified module in its standard library and other directories. If the module is not found, the “No Module Named…” error is triggered.

Syntax:

ModuleNotFoundError: No module named

Below, are the reasons for occurring ModuleNotFoundError: No module named Error in Python:

  • Module Not Installed
  • Incorrect Module Name
  • Incorrect Directory Structure

Module Not Installed

The most common reason for this error is that the required module is not installed on the system. Python relies on external packages and modules to extend its functionality, and if a module is not installed, Python cannot find it during the import.

Python3




# Error: ModuleNotFoundError
import requests


Output

Hangup (SIGHUP)
Traceback (most recent call last):
  File "Solution.py", line 2, in <module>
    import requests
ModuleNotFoundError: No module named 'requests

Incorrect Module Name

Typos or incorrect module names in the import statement can also lead to this error. Python is case-sensitive, so a slight difference in the module name will result in a “No Module Named…” error.

Python3




import jso


Output

Hangup (SIGHUP)
Traceback (most recent call last):
  File "Solution.py", line 1, in <module>
    import jso
ModuleNotFoundError: No module named 'jso'

Incorrect Directory Structure

If the module is part of your project but is not in the correct directory or is not properly structured, Python may not be able to locate it. Ensuring the correct directory structure and proper packaging is essential to avoid this error.

Python3




# Error: ModuleNotFoundError
from myproject.mymodule import myfunction


Output

Hangup (SIGHUP)
Traceback (most recent call last):
  File "Solution.py", line 2, in <module>
    from myproject.mymodule import myfunction
ModuleNotFoundError: No module named 'myproject'

Solution for ModuleNotFoundError: No module named in Python

Below, are the approaches to solve ModuleNotFoundError in Python:

  • Installed Module
  • Correct Module Name
  • Correct Directory Structure

Installed Module

To resolve this issue, you need to install the missing module using a package manager like pip. Open your terminal or command prompt and run the following command:

pip install <module_name> or pip install requests

Replace <module_name> with the name of the missing module.

Correct Module Name

Double-check the import statement for typos or incorrect capitalization. Ensure that the module name matches the actual module name.

Python3




# Incorrect
import jso
 
# Correct
import json


Correct Directory Structure

Make sure your module is in the correct directory and has the right structure. If your module is part of a package, ensure that the package is properly structured, and use the correct import statement.

Python3




# Incorrect
from myproject.mymodule import myfunction
 
# Correct
from myproject.package.mymodule import myfunction


Conclusion

In conclusion , the ModuleNotFoundError error in Python can be frustrating, but with a systematic approach, it can be easily resolved. By identifying whether the module is not installed, the module name is incorrect, or the directory structure is improper, you can apply the appropriate solution. Always double-check your code, ensure proper installation of required modules, and maintain a clean and organized project structure to minimize the chances of encountering this error.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads