Solve - ImportError: cannot import name X in Python

avatar

Borislav Hadzhiev

Last updated: May 1, 2022

banner

Check out my new book

Solve - ImportError: cannot import name X in Python #

The Python "ImportError: cannot import name" occurs when we have circular imports (importing members between the same files). To solve the error, move the objects to a third file and import them from a central location in other files, or nest one of the imports in a function.

importerror cannot import name

Here is an example of having circular imports (importing members between the same files).

This file is called main.py and imports a function from another.py.

main.py
from another import do_multiplication # 👈️ imports from another.py def do_addition(a, b): return a + b print(do_multiplication(5, 5))

And here is the code for another.py.

another.py
from main import do_addition # 👈️ imports from main.py def do_multiplication(a, b): return a * b # ⛔️ ImportError: cannot import name 'do_multiplication' from partially initialized module 'another' (most likely due to a circular import) (/home/borislav/Desktop/bobbyhadz_python/another.py) print(do_addition(5, 5))

We are importing members between the two files, so they depend on one another.

This is very confusing for the Python interpreter and should be avoided.

Instead, you can move the functions to a third file, and import them from a central location in your other files, without having to deal with circular imports.

Here is the updated code for third.py.

third.py
def do_addition(a, b): return a + b def do_multiplication(a, b): return a * b

And now we are able to import the two functions in any other file without having circular imports.

main.py
from third import do_addition, do_multiplication print(do_addition(5, 5)) # 👉️ 10 print(do_multiplication(5, 5)) # 👉️ 25

Now the interpreter won't get confused when running our code because we don't have imports between the same files.

An alternative solution is to delay running the import statement in one of the files by nesting it inside of a function.

Here is the code for main.py.

main.py
from another import do_multiplication def do_addition(a, b): return a + b print(do_multiplication(5, 5))

The file imports a function from another.py at the top level.

And here is the code for another.py which nests the import statement inside of a function.

another.py
def do_multiplication(a, b): return a * b def import_in_function(): # 👇️ import statement is now in a function from main import do_addition print(do_addition(5, 5)) import_in_function()

The import statement is now in a function and not at the top level, so it isn't ran until we invoke the function.

This solves the error because the Python interpreter isn't trying to run two files at the same time that depend on one another.

Instead, the import statement is ran when the function is invoked.

However, this can be confusing for other people reading your code and it is much more intuitive to move the objects into a separate file and import them from the third file.

For example, if you have files a.py and b.py, move the objects in a file called c.py and import the objects from c.py to avoid any circular imports.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.