Marim
Marim

Reputation: 11

error importing files in python

i have two modules Book Class and Author Class .The author class module imports the book class module as follows

from BookClass import Book
from PersonClass import Person

class Author(Person):

and the book class module also imports the author class module as follows

from AuthorClass import Author

class Book:

when i run any of the two modules it gives me an import error.I am not sure of how to fix this error. Thanks in advance.

Upvotes: 0

Views: 45

Answers (1)

BoarGules
BoarGules

Reputation: 16942

You are getting this error because your book class module says

from AuthorClass import Author

Remember that import is an executable statement. When the interpreter executes that statement, the first thing it does is import this code:

from BookClass import Book

but at that moment the class Book isn't defined yet, because the definition of Book comes after from AuthorClass import Author.

You have two classes with mutual references, and I take it you have complete control over the module structure. If I were in that position I would make the problem go away by putting both class definitions in the same module.

Upvotes: 1

Related Questions