1

Updated Problem solved, I have some design problem here.

The directory looks like that:

/view
  |-__init__.py
  |-quiz.py
  |-test.py
  |-user.py

And the problem is that in quiz.py, I import a class from test. and in test.py, I import a class from quiz .

Updated: I changed import but there is still a AttributeError

The code as following:

quiz.py

#ignore some imports here
import test
from user import User

class Quiz(Document):
    creator         =   ReferenceField(User, reverse_delete_rule=CASCADE)
    info            =   GenericEmbeddedDocumentField("QuizInfo")
    description     =   StringField(max_length=100)
    attachment      =   GenericEmbeddedDocumentField("QuizAttach")
    correctanswer   =   GenericEmbeddedDocumentField("QuizAnswer")
    wronganswer     =   GenericEmbeddedDocumentField("QuizAnswer")
    manualdifficulty=   FloatField(min_value=0, max_value=1)
    autodifficulty  =   FloatField(min_value=0, max_value=1)
    checkout        =   GenericEmbeddedDocumentField("QuizCheckcout")
    tag             =   ListField(StringField(max_length=20))

#ignore some codes here

class QuizCheckout(EmbeddedDocument):
    time            =   DateTimeField()
    type            =   IntField()
    description     =   StringField()
    test            =   ReferenceField(test.Test, reverse_delete_rule=CASCADE)

test.py

import quiz


class Test(Document):
    createdate      =   DateTimeField()             #Create datetime
    description     =   StringField()               #decription of this test
    takennumber     =   IntField()                  #the number of students who may take this test
    quiz            =   GenericEmbeddedDocumentField('TestQuiz')

class TestQuiz(EmbeddedDocument):
    quiz            =   ListField(ReferenceField(quiz.Quiz, reverse_delete_rule=CASCADE))
                        #Reference to Quiz, if Quiz is deleted, this reference will be deleted too.
    correct         =   IntField()
                        #how many students got this right

and the error is

Exception Type: AttributeError Exception
Value: 'module' object has no attribute 'Quiz'

At first I thought that maybe a recursive problem, but I only find that I could move import into functions to avoid recursive import, but there is no functions here, and I try to move import into class, it don't work.

Is there any way to keep these definition in separate file?

hymloth
  • 6,869
  • 5
  • 36
  • 47
bxshi
  • 2,252
  • 5
  • 31
  • 50
  • possible duplicate of [Circular (or cyclic) imports in Python](http://stackoverflow.com/questions/744373/circular-or-cyclic-imports-in-python) – simonzack Jul 26 '14 at 08:06

3 Answers3

3

This is a classical cyclic import situation. Instead of using "from test import Test", you can simply "import test" and then access Test by test.Test. For more info see this stackoverflow question.

Community
  • 1
  • 1
hymloth
  • 6,869
  • 5
  • 36
  • 47
  • I changed it and there is another error here. `Exception Type: AttributeError Exception Value: 'module' object has no attribute 'Quiz'` – bxshi Mar 30 '12 at 10:31
  • 1
    @bxshi Also change `from quiz import Quiz` to `import quiz` – Janne Karila Mar 30 '12 at 10:40
  • @JanneKarila yes, I change `from quiz import Quiz` to `import quiz`, and change `from test import Test` to `import test`, and it comes out a `AttributeError` – bxshi Mar 30 '12 at 10:43
1

Move QuizCheckout to a separate module. (QuizCheckout references Test at the class definition level, and Test references Quiz, that is the root of the problem)

Janne Karila
  • 24,266
  • 6
  • 53
  • 94
0

If what hymloth writes is correct (I did not try it out) you should also be able to use the name "Test" again by doing this:

import test
Test = test.Test
katzenversteher
  • 810
  • 6
  • 13
  • yeah, but with his instruction, there comes another error, it told me that `module object has no attribute 'Quiz'` how is that? Did some other configure should be done? – bxshi Mar 30 '12 at 10:36
  • You could start the debugger after the import by inserting this: "import pdb; pdb.set_trace()" and then once the breakpoint is triggered you can do "dir(Quiz)" to check what's going on. Hope it works! – katzenversteher Mar 30 '12 at 10:55