Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uvicorn fails "quietly" upon a circular import error #2035

Closed
Kludex opened this issue Jul 8, 2023 Discussed in #2034 · 4 comments
Closed

Uvicorn fails "quietly" upon a circular import error #2035

Kludex opened this issue Jul 8, 2023 Discussed in #2034 · 4 comments

Comments

@Kludex
Copy link
Member

Kludex commented Jul 8, 2023

Discussed in #2034

Originally posted by mikeedjones July 8, 2023
From this comment and discussion.

When there is a circular import in the app, uvicorn reports ERROR: Error loading ASGI app. Could not import module "main". without exposing the stacktrace which would be reported if you ran the code using python main.py. MRE below.

# main.py
from bar import Bar

class Foo:
    pass

async def app(scope, receive, send):
    assert scope['type'] == 'http'
#bar.py
from main import Foo

class Bar:
    pass

Running with

uvicorn main:app

only results in

ERROR:    Error loading ASGI app. Could not import module "main".

whereas running

python main.py

reports the far more useful

ImportError: cannot import name 'Bar' from partially initialized module 'bar' (most likely due to a circular import) (/workspaces/fastapi/bar.py)

Not sure how you'd start debugging this one I'm afraid - or if its even a "bug". For comparison syntax errors are reported correctly.

@mikeedjones
Copy link
Contributor

I'll take a run at this :)

@Kludex
Copy link
Member Author

Kludex commented Jul 8, 2023

I'll take a run at this :)

Thanks.

The problem is here:

try:
module = importlib.import_module(module_str)
except ImportError as exc:
if exc.name != module_str:
raise exc from None
message = 'Could not import module "{module_str}".'
raise ImportFromStringError(message.format(module_str=module_str))

@mikeedjones
Copy link
Contributor

mikeedjones commented Jul 9, 2023

#2038 tries to address this

@Kludex
Copy link
Member Author

Kludex commented Jul 9, 2023

It will be available on 0.23.0.

@Kludex Kludex closed this as completed Jul 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants