Open In App

Importerror: Cannot Import Name In Python

Last Updated : 02 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

One common error that developers encounter is the “ImportError: Cannot Import Name.” This error occurs when the Python interpreter is unable to import a particular name or module within your code. In this article, we’ll explore what causes this error and provide solutions to fix it.

What is ImportError: Cannot Import Name?

The “ImportError: Cannot Import Name” error typically occurs when there is a circular import or a dependency loop in your Python code. Circular imports happen when two or more modules depend on each other, creating a loop that confuses the interpreter. As a result, Python raises an ImportError because it cannot determine the correct order of module imports.

Syntax:

Importerror: Cannot Import Name 

Why does Importerror: Cannot Import NameError Occurs?

Below, are the reason that’s why Importerror: Cannot Import Name occurs in Python:

  • Circular Import
  • Incorrect Module Reference
  • Typo in Import Statement

Circular Import

Below, the code consists of two Python modules, `module_a.py` and `module_b.py`. `module_a` imports `function_b` from `module_b`, and `module_b` imports `function_a` from `module_a`, creating a circular dependency. This circular import structure can lead to the “ImportError: Cannot Import Name” when attempting to use functions from either module.

module_a.py

Python3




# module_a.py
from module_b import function_b
 
def function_a():
    print("Function A")


module_b.py

Python3




# module_b.py
from module_a import function_a
 
def function_b():
    print("Function B")


Output:

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

Incorrect Module Reference

In below, code `main.py` attempts to import `my_function` from `mymodule`, but `mymodule.py` defines a function named `another_function`. This inconsistency causes an `ImportError: Cannot Import Name` when calling `my_function()` in `main.py`.

main.py

Python3




# main.py
from mymodule import my_function
 
my_function()


mymodule.py

Python3




# mymodule.py
def another_function():
    print("Another Function")


Output:

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

Typo in Import Statement

In `myscript.py`, an attempt to import and execute `my_function` from `utils` causes an `ImportError: Cannot Import Name` because the function is defined later in `utils.py`. To resolve, switch the order of the import statement and function definition in `utils.py`.

myscript.py

Python3




# myscript.py
from utils import my_function
 
my_function()


utils.py

Python3




# utils.py
def my_function():
    print("My Function")


Output:

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

Solve Importerror: Cannot Import Name In Python

Below, are the three approaches to solve Importerror: Cannot Import Name in Python:

  • Circular Import
  • Incorrect Module Reference
  • Typo in Import Statement

Circular Import

To resolve circular imports, reorganize your code to eliminate the circular dependency. You can move the shared functionality to a separate module or refactor the code to break the circular link. as below code , This ensures that the import occurs when the functions are called, preventing the circular dependency at the module level.

module_a.py

Python3




# module_a.py
def function_a():
    from module_b import function_b
    print("Function A")
    function_b()


module_b.py

Python3




# module_b.py
def function_b():
    from module_a import function_a
    print("Function B")
    function_a()
 
function_b()


Output:

Function B
Function B
Function A
Function A

Correct Module Reference

Ensure that your import statements correctly reference the intended module and its names. In the second example, correcting the import statement in main.py to from mymodule import another_function will resolve the issue.

main.py

Python3




# main.py
from mymodule import another_function  # Corrected import statement
 
another_function()


mymodule.py

Python3




# mymodule.py
def another_function():
    print("Another Function")


Output:

Another Function

Typo in Import Statement

Check for typos or spelling errors in your import statements. In the third example, correcting the import statement in myscript.py to from utils import my_function will fix the ImportError.

utils.py

Python3




# utils.py
def my_function():
    print("My Function")


myscript.py

Python3




# myscript.py
from utils import my_function
 
my_function()


Output:

My Function

Conclusion

Handling the ImportError: Cannot Import Name in Python involves careful examination of your code for circular dependencies, correct module references, and import statement typos. By understanding the nature of the error and applying appropriate solutions, you can ensure a smoother development experience and more robust Python applications.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads