[Python] How to solve Circular import error in Python

Discussão em 'Python' iniciado por Stack, Agosto 12, 2025.

  1. Stack

    Stack Membro Participativo

    I have this User.py class

    from .Post import Post
    from .Comment import *
    from typing import List
    import strawberry
    from datetime import datetime
    @strawberry.type
    class User:
    id: str
    email: str
    username: str | None
    posts: List[Post]
    comments: List[Comment]
    createdAt: datetime
    updatedAt: datetime


    and this is Post.py Class

    from .User import *
    import strawberry
    from datetime import datetime
    @strawberry.type
    class Post:
    _id: str
    userId: str
    user: User | None # Relationship
    content: str
    createdAt: datetime
    updatedAt: datetime


    they are both in same folder, I have this error ImportError: cannot import name 'Post' from partially initialized module 'GraphQl.models.Post' (most likely due to a circular import) (U:\projets\grats-backend\GraphQl\models\Post.py)

    Someone suggested the impot * can solve the issue but this was not really the case, I appreiate any help, thank you

    Continue reading...

Compartilhe esta Página