Skip to content

Circular import in target file accidentally triggers 'No module named ... found' #2757

@Bluenix2

Description

@Bluenix2

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

While developing it appears that I accidentally caused a circular import which Python traces back to starting in the file I have my app in. As a result, Python outputs an error such as the following:

ImportError: cannot import name 'constants' from partially initialized module 'app' (most likely due to a circular import) (/api/app/__init__.py)

In this case my module I pass to the sanic server is app:app, from within /api.

Code snippet

No response

Expected Behavior

I had this in the back of my mind the entire time, but found it very difficult to troubleshoot due to Sanic swallowing the error. As a result I ended up the rabbit hole of accidental breaking changes and tried commenting out different changes. An hour later I finally found the right import.

It would help if Sanic continued to output the specific import error, on the off-chance that it isn't an incorrectly setup module. The alternative would be to use more fine-grained importlib and manually call some functions rather than use their help functions. As a result there should be a different call which finds the file (an ImportError here hints at an incorrectly setup module), than the one which loads it (user error).

How do you run Sanic?

Sanic CLI

Operating System

Windows (Docker, Python:3.11)

Sanic Version

23.3

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions