[Python-ideas] ImportError raised for a circular import

Matt boehm.matthew at gmail.com
Tue Jun 13 18:38:56 EDT 2017


I've also been thinking about this lately. I can remember being confused
the first time I saw "ImportError: cannot import name X". As there are
multiple things that can cause this error, it took me a while to find a
stackoverflow post that suggested that this might be due to circular
imports. After learning this, I still had to read a few sources to
understand what circular imports were and how to fix the problem.

A quick stackoverflow search reveals that people frequently have questions
about this error message:
https://www.google.com/search?q=stackoverflow+python+import+error&oq=stackoverflow+python+import+error&aqs=chrome..69i57j0j69i64.6423j0j7&sourceid=chrome&ie=UTF-8#q=site:stackoverflow.com+python+importerror+%22cannot+import+name%22


At the very least, it would be nice if the error message could
differentiate between different causes for this error. Ideally, however,
I'd love if for circular imports, it included text on what they are and how
to resolve them.

On Tue, Jun 13, 2017 at 6:10 PM, Mahmoud Hashemi <mahmoud at hatnote.com>
wrote:

> I didn't interpret the initial email as wanting an error on *all* circular
> imports. Merely those which are unresolvable. I've definitely helped people
> diagnose circular imports and wished there was an error that called that
> out programmatically, even if it's just a string admonition to check for
> circular imports, appended to the ImportError message.
>
> On Tue, Jun 13, 2017 at 1:43 PM, Chris Angelico <rosuav at gmail.com> wrote:
>
>> On Wed, Jun 14, 2017 at 6:35 AM, Barry <barry at barrys-emacs.org> wrote:
>> > On 13 Jun 2017, at 20:13, Antoine Rozo <antoine.rozo at gmail.com> wrote:
>> >
>> > But circular imports are sometimes needed in modules.
>> > For example when you have two classes in two different modules that
>> > reference each other in their methods (and because you can't pre-declare
>> > classes like in C++).
>> >
>> >
>> > Really? It has always been a strong sign of a design bug in all the
>> cases I
>> > have ever seen.
>> > The example you suggest always fails when I accidentally write it.
>> >
>> > Pylint will certainly shout loud that this case is an error.
>> >
>>
>> Depends on your definition of "circular". Consider this:
>>
>> # __init__.py
>> from flask import Flask
>> app = Flask(__name__)
>> from . import views
>>
>> # views.py
>> from . import app
>> @app.route("/")
>> def home():
>>     ...
>>
>>
>> Technically this is circular. During the loading of __init__, views
>> will be imported, which then imports something from __init__. But it's
>> perfectly well-defined (there's no way that views will ever be the
>> first one imported, per the rules of packages) and it makes good
>> sense. An error on circular imports, or even a warning, would be very
>> annoying here.
>>
>> ChrisA
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170613/90dcfaf1/attachment-0001.html>


More information about the Python-ideas mailing list