Fixing Python ImportError: cannot import name ‘…’ from partially initialized module ‘…’ (most likely due to a circular import)

Python ImportError: cannot import name ‘…’ from partially initialized module ‘…’ (most likely due to a circular import) is an error which occurs sometimes when there is a naming conflict caused by the name of your file.

Today, I will explain why this error takes place and how to fix it, while also presenting the steps in detail and adding other solutions that could solve the error.

Exploring the Python ImportError: cannot import name ‘…’ from partially initialized module ‘…’ (most likely due to a circular import)

This is an error which occurs sometimes when there is a naming conflict caused by the name of your file.

Bellow is the error message, please make sure it is the right one.

                                                                       #
ImportError: cannot import name 'get_sentinel' from partially initialized module 'authentication.models' 
(most likely due to a circular import) (/home/eduardo/projdevs/upgrade-intra/authentication/models.py)**
                                                                       #

Bellow, I am going to explain the cause of the problem and propose multiple different solutions.

Solution 1 : Fix naming conflicts

When there is a naming conflict caused by the name of your file, a circular import is the result of that.

This takes place a lot when you name a file packageA.py for example, you have a package in your project called packageA.

And you make things worst by importing that package from inside packageA.py. Just like this.

                                                                       #
from packageA import packageA
                                                                       #

This will result in the error we are trying to solve, he solution is to obviously rename the file.

If that does not solve your issue, please try the method bellow.

Solution 2 : Fix circular imports

The second option is to look for possible circular imports and solve them.

In this example we have a package called packageA which has a subpackage that contains two python files, fileA and __init__.py.

We also have a main directory in which we have another file.

circular import

If we assume we are inside fileB and we want to import from fileA then we should do it this way

                                                                       #
from ..subpackageA.fileA import importedobject
                                                                       #

And now we have avoided any kind of circular import that might result from importing (by mistake) a package called importedobject.

I hope the fix above fixed your problem. Thanks for reaching the end of this blog post/article.

Summing-up : 

That is it guys, this is the end of this article/guide, I hope you found it useful in solving your particular problem, you can support our work on our Kofi account.

Thank you for reading my blog post to the end, If you want to learn more about the Python programming language, check out the official Python Documentation : https://docs.python.org/3/