0

i'm having a problem with imports: i have this folder structure:

Q2AInterface
    __init__.py
    Q2A.py
    Question.py
    Like.py
main.py

And to make it short the content are something like this:

Q2A.py:

from . import Question
from . import Like
class Q2A:
    pass

Question.py:

from . import Q2A
from . import Like

class Question:
    pass

Like.py:

from . import Question
class Like:
    question = Question.Question()

main.py:

#!/usr/bin/python3
from Q2AInterface import Q2A,Like,Question

Problem is that in Likes.py it gives me error when i use the Question class, the error is:

module 'Q2AInterface.Question' has no attribute 'Question'

I really have no idea how to fix this, i've tried writing the imports in every way i could think of, i tried:

import Question, import Question.Question, from .Question import Question, from . import Question.

i really have no idea what to do, tried reading some questions online too, but no matter what i try, init, or different imports, it just won't work...

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
Celeste Capece
  • 1,306
  • 2
  • 11
  • 20

1 Answers1

1

you've landed in a python circular importing problem !

basically in Question.py remove from . import Like, OR else, if you need it, put it after the class, like so.

from . import Q2A

class Question:
    pass

from . import Like
Aditya Shankar
  • 702
  • 6
  • 12
  • I imagined it was something like that but... what if Question uses Like inside? Basically a question has a list of Likes, and a Likes need to know it's parent Question. – Celeste Capece Nov 14 '19 at 03:22
  • 1
    @FedericoCapece the code will not throw an error (because the contents are not initialized as long as the contents are actually created), as long as Like is imported. so its fine if Question uses Like – Aditya Shankar Nov 14 '19 at 05:26