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:
>>> import non_existing_module
Traceback (most recent call last):
...
ModuleNotFoundError: No module named 'non_existing_module'
An example of how to handle the exception:
>>> 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.
Related Resources
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.
For additional information on related topics, take a look at the following resources:
- Python import: Advanced Techniques and Tips (Tutorial)
- Python Exceptions: An Introduction (Tutorial)
- Python's Built-in Exceptions: A Walkthrough With Examples (Quiz)
- Advanced Python import Techniques (Course)
- Python import: Advanced Techniques and Tips (Quiz)
- Introduction to Python Exceptions (Course)
- Raising and Handling Python Exceptions (Course)
- Python Exceptions: An Introduction (Quiz)