The python ImportError: cannot import name error occurs when the import class is inaccessible or the imported class in circular dependence. The import keyword is used to load class and function. The keyword from is used to load the module. For any reason, if the import class is not available in the python class path, The “ImportError: cannot import name” python error is thrown.

The following are the reasons for the ImportError: cannot import name

  • The import class is not available or not created.
  • The import class name is mis-named or mis-spelled
  • The import class name and module name is mis-placed.
  • The import class is not available in python class path
  • The import class is not available in python library
  • The import class is in circular dependency

The python module is just a python file with the .py extension. The keyword from will be used to load the python module. A class in a python module is imported using the keyword import. If the imported class is not in the referred python file, the python interpreter will throw the error ImportError: Cannot import name.

If two python files refer to each other and attempt to load the other file, it will create the circular load dependence. That will cause error in heap memory. If the python interpreter detects the circular dependence, it throws the error ImportError: Can’t Import Name.



Exception

The python interpreter will display the import error similar to the following

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 1, in <module>
    from Student import Student
ImportError: cannot import name Student
[Finished in 0.1s with exit code 1]


Root Cause

The python interpreter tries to locate and load the class specified in import statement. If the class name is inaccessible or unavailable, then this error will be thrown. There are numerous reasons for failing to locate or load the class. If the import class is available and loaded, it will fix this problem.

The other reason is that the load dependence is circular. If two python files refer to each other and try to load the other file, then the circular load dependency will be established. That will cause heap memory error. Python detects and throws the error ImportError: Can’t Import Name.



How to reproduce this issue

The error “ImportError: cannot import name” can be reproduced if you add a class name in the import statement that does not exist. The python interpreter will search for the class name in the python module. The class definition is not available in the python file. Therefore, python interpreter is going to throw this error.

In the example below, the Student.py file includes an Employee Class. The test.py file will import a student class from the student.py file. As the student.py file does not contain the student class, the python interpreter will throw this import error.

Student.py

class Employee:
	id = 1

test.py

from Student import Student

st = Student();
print(st.id);

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 1, in <module>
    from Student import Student
ImportError: cannot import name Student
[Finished in 0.1s with exit code 1]


Solution 1

The imported class will not be created or available in the python file. Check the import class in the python file. If this is not available, create a class in the python file. The python interpreter loads the class from the python file. This will resolve the “ImportError: Can not Import Name” error.

Student.py

class Student:
	id = 1

test.py

from Student import Student

st = Student();
print(st.id);

Output

1


Solution 2

The name of the import class may not be correct in the import statement. Verify the name of the class in the python file, correct the name of the class in the import statement. This could be due to spelling incorrectly in the import statement. Make sure the name of the class in the python file and the name of the class in the import statement are the same. This is going to fix the error.

Student.py

class Employee:
	id = 1

test.py

from Student import Employee

emp = Employee();
print(emp.id);

Output

1


Solution 3

The import class may not be available in the class you are looking for. The import class can be declared in a python file. The import statement can be searched in another python file. Verify the name of the module declared in the “from” keyword. The name of the module and the name of the python file must be the same. If different, either change the name of the file or change the name of the module.

Employee.py

class Employee:
	id = 1

Student.py

class Student:
	id = 1

test.py

from Student import Employee

emp = Employee();
print(emp.id);

Exception

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 1, in <module>
    from Student import Employee
ImportError: cannot import name Employee
[Finished in 0.1s with exit code 1]

Solution – test.py

from Employee import Employee

emp = Employee();
print(emp.id);


Solution 4

The common error is that the import class name and module name is mis-placed. In most of the time, the python class name and the python file name are same differs with camel case. If the class name and module name is misplaced in the import and from statement, then this error will occur.

employee.py

class Employee:
	id = 1

test.py – Error

from Employee import employee

emp = Employee();
print(emp.id);

The class name and module name is misplaced with Employee and employee. The correct import statement is as below

test.py – Solution

from employee import Employee

emp = Employee();
print(emp.id);


Solution 5

If two python files refer to each other and attempt to load the other file, it will create the circular load dependence. That will cause error in heap memory. If the python interpreter detects the circular dependence, it throws the error ImportError: Can’t Import Name.

Employee.py

from Student import Student
class Employee:
	id = 1

Student.py

from Employee import Employee
class Student:
	id = 1

In the above example Employee Module tries to load Student class and Student Module tries to load Employee class. This creates the circular dependency. Remove the circular dependency. This will resolve the import error.



Leave a Reply