AppDividend
Latest Code Tutorials

Python cannot import name: How to Solve ImportError

1

Python implements at least three different ways to import modules. You can use the import statement, the from statement, or the built-in __import__ function. Modules are performed during import, and new functions and classes won’t see in the module’s namespace until the def (or class) statement has been executed.

Python cannot import name

To solve ImportError: cannot import name in Python, solve the circular dependencies, and defer imports. To solve circular dependencies, use the module in a function when we needed it. Breaking a circular dependency makes the code cleaner, understandable and gives easy access to all methods requiring dependency.

Code Snippet

See the below snippet, which eliminates the circular dependencies.

import WhateverModule


def WhateverFunction(arg):
    from some.dependency import DependentClass

Python can detect circular dependencies and prevent the infinite loop of imports. Basically, what happens is that an empty placeholder is created for the module. Once the circularly dependent modules are compiled, it updates the imported module.

To make logic clear is very important. This problem appears because the reference becomes a dead loop. Let’s take an example of circular dependencies.

Let’s define a y.py file with the following code.

from x import x1

def y1():
    print('y1')
    x1()

def y2():
    print('y2')

if __name__ == '__main__':
    y1()

This file is using the function imported from the x.py file.

Now, let’s define the x.py file.

from y import y2

def x1():
    print('x1')
    y2()

Now, this file x.py is dependent on y.py. That means the x.py file is dependent on y.py. You can see the circular dependencies.

Finally, if you run the file y.py file, you can see the following code.

Traceback (most recent call last):
  File "/Users/krunal/Desktop/code/pyt/database/y.py", line 1, in <module>
    from x import x1
  File "/Users/krunal/Desktop/code/pyt/database/x.py", line 1, in <module>
    from y import y2
  File "/Users/krunal/Desktop/code/pyt/database/y.py", line 1, in <module>
    from x import x1
ImportError: cannot import name 'x1' from partially initialized module 'x' 
(most likely due to a circular import) (/Users/krunal/Desktop/code/pyt/database/x.py)

And we get the following error.

ImportError: cannot import name ‘x1’ from partially initialized module ‘x’.

To resolve the ImportError: Cannot import name, modify the x.py file. Instead of importing the y module at the start of the x.py file, write at the end of the file.

def x1():
    print('x1')
    y2()

from y import y2

Now rerun, and you can see the following output.

python3 y.py
y1
x1
y2

We have solved this problem by using the import module or class or function where we needed it. If we use this approach, we can fix circular dependency.

That is it resolving ImportError: Python cannot import name.

1 Comment
  1. Karim Pérez Martínez says

    Thanks! Simple and useful!

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.