ImportError: cannot import name

The python error “ImportError: cannot import name” occurs when you import a class in to a python file that is not available in the python module. The “import” keyword is used for loading a class and function. For loading a module the keyword “from” is used. The error is because the import configuration is invalid

A class in a python file is imported into another file using the keyword “import”. The python module is just a python file with the “.py” extension. If the imported class is not in the python code, this error “ImportError: Cannot import name” is thrown.

The import declaration is used for loading a class from another python file. The python file is configured using keyword “from.” The class name is imported using keyword “import.” If the class is not available in the python file, or can not load the class, the python interpreter will throw the error “ImportError: cannot import name.”

There are three possible reasons in the code for seeing this error. The interpreter can not invoke the class specified in the import statement. The python error shows up as the one below

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 root cause of the error is that the python interpreter can not locate the class description defined in the declaration on import. There are different reasons for not having the definition of class. If the import class is available and loaded this problem is fixed.

The import class may not be available in the class. Or the python module can not be right where the class is being searched. The class name may be spelled incorrectly.



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);



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *