Newbie - circular import problem

Diez B. Roggisch nospam-deets at web.de
Tue Feb 24 11:51:28 CET 2004


> --a.py--
> import b
> class ClassA:
>     def __init__(self):
>         print 'ClassA'
> 
> --b.py--
> import a
> class ClassB(ClassA):
>     def __init__(self):
>         print 'ClassB'
>  
> If I try to access ClassB (for example, by firing up python and importing
> b), I get the error:
> 
>   File "b.py", line 2, in ?
>     class ClassB(ClassA):
>     NameError: name 'ClassA' is not defined
> 
> Could someone explain what is going wrong here? I'm still getting up to
> speed with python, but, to me, this looks like the import of b is occuring
> before ClassA has been defined. Is that right? How can I correct this,
> apart from amalgamating the two into a single file?

Put whatever a needs from b (its obviously not ClassB) into a module c, and
import it from both.

-- 
Regards,

Diez B. Roggisch



More information about the Python-list mailing list