ModuleNotFoundError

ModuleNotFoundError is a built-in exception that Python raises when an import statement fails to find a specified module.

This exception is a subclass of ImportError and it provides a more specific error message when a module can’t be found.

You might run into this exception if you try to import a module that isn’t installed or is otherwise unavailable in your current Python environment.

ModuleNotFoundError Occurs When

  • Importing a module that isn’t installed in your Python environment
  • Trying to load a custom module that’s not located in your project’s directory or Python’s import path

ModuleNotFoundError Examples

An example of when this exception appears:

Python
>>> import non_existing_module
Traceback (most recent call last):
    ...
ModuleNotFoundError: No module named 'non_existing_module'

An example of how to handle the exception:

Python
>>> try:
...     import non_existing_module
... except ModuleNotFoundError:
...     print("The module isn't installed. Please install it to proceed.")
...
The module isn't installed. Please install it to proceed.

You typically don’t raise ModuleNotFoundError manually.

Tutorial

Python's Built-in Exceptions: A Walkthrough With Examples

In this tutorial, you'll get to know some of the most commonly used built-in exceptions in Python. You'll learn when these exceptions can appear in your code and how to handle them. Finally, you'll learn how to raise some of these exceptions in your code.

intermediate python

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated March 17, 2025 • Reviewed by Martin Breuss