Back to Blog
Developer guide
PYTHONJune 29, 2026

Python ImportError and ModuleNotFoundError Guide: Fixing Common Issues

As a Python developer, you've probably encountered the frustrating ImportError and ModuleNotFoundError at some point. These exceptions can occur when your code relies on external modules or packages that are not properly installed or imported. In this guide, we will walk you through the common causes and solutions for ImportError and ModuleNotFoundError, helping you to identify and fix these issues in your Python projects.

1. ImportError

An ImportError occurs when Python is unable to import a module or package. This can happen if the module is not installed, there's a spelling mistake in the import statement, or the module is not accessible due to permission issues.

Why It Happens

The most common causes of ImportError include incorrect module names, missing module installations, or incorrect import statements.

How to Fix It

To fix an ImportError, first ensure that the module is installed by running pip install <module_name>. Next, check the import statement for spelling mistakes and correct them if necessary. Finally, verify that the module is accessible by checking the file path and permissions.


2. ModuleNotFoundError

A ModuleNotFoundError is thrown when Python cannot find a module or package. This can happen if the module is not installed or if there's a circular import issue.

Why It Happens

The most common causes of ModuleNotFoundError include missing module installations or circular import issues.

How to Fix It

To resolve a ModuleNotFoundError, ensure that the module is installed by running pip install <module_name>. Additionally, check for circular import issues and refactor your code to avoid them.


3. ImportError: No module named <module_name>

An ImportError with the specific message 'No module named <module_name>' occurs when Python is unable to find a module with the specified name.

Why It Happens

This error typically happens when there's a spelling mistake in the import statement or when the module is not installed.

How to Fix It

To fix this error, first check the import statement for spelling mistakes and correct them if necessary. Then, ensure that the module is installed by running pip install <module_name>.


4. ImportError: cannot import name <function_name>

An ImportError with the specific message 'cannot import name <function_name>' occurs when Python is unable to find a function or variable within a module.

Why It Happens

This error typically happens when there's a spelling mistake in the import statement or when the function or variable does not exist in the module.

How to Fix It

To resolve this error, first check the import statement for spelling mistakes and correct them if necessary. Then, verify that the function or variable exists in the module and correct any issues.


5. ModuleNotFoundError: No module named <module_name>

A ModuleNotFoundError with the specific message 'No module named <module_name>' occurs when Python is unable to find a module with the specified name.

Why It Happens

This error typically happens when there's a spelling mistake in the import statement or when the module is not installed.

How to Fix It

To fix this error, first check the import statement for spelling mistakes and correct them if necessary. Then, ensure that the module is installed by running pip install <module_name>.


6. ImportError: Importing module <module_name> failed

An ImportError with the specific message 'Importing module <module_name> failed' occurs when Python is unable to import a module due to an underlying issue.

Why It Happens

This error typically happens when there's an issue with the module's installation or the system's Python environment.

How to Fix It

To resolve this error, first try reinstalling the module using pip install <module_name>. If the issue persists, check the system's Python environment and ensure that it's properly configured.


7. ImportError: Cannot import name <function_name> from <module_name>

An ImportError with the specific message 'Cannot import name <function_name> from <module_name>' occurs when Python is unable to find a function or variable within a module.

Why It Happens

This error typically happens when there's a spelling mistake in the import statement or when the function or variable does not exist in the module.

How to Fix It

To fix this error, first check the import statement for spelling mistakes and correct them if necessary. Then, verify that the function or variable exists in the module and correct any issues.

Conclusion

In conclusion, ImportError and ModuleNotFoundError are common issues that Python developers face when working with external modules or packages. By understanding the causes and solutions outlined in this guide, you can effectively troubleshoot and resolve these exceptions in your projects, ensuring a smooth development experience.

Explore More Debugging Resources

- [Browse all PYTHON errors](/languages/python)

- [Browse errors by type](/error-types)

- [Search all documented errors](/search)

- [Use the Error Explainer](/error-explainer-tool)

Browse allPython errors

Related PYTHON Articles

Have a specific error? Get an instant explanation.

Explain an Error