Newbie - circular import problem

S

sw

Can you give me some pointers on resolving this circular inclusion? I've got
two classes, in two files:

--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?

Many thanks for any guidance.
 
D

Diez B. Roggisch

--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.
 
P

Peter Otten

sw said:
Can you give me some pointers on resolving this circular inclusion? I've
got two classes, in two files:

--a.py--
import b
class ClassA:
def __init__(self):
print 'ClassA'

--b.py--
import a

Your problem has nothing to do with circular references. Just change the
following to

class ClassB(a.ClassA):
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?

Many thanks for any guidance.

Circular imports are often an indication of a design error. However, most of
the time python has no problem with two modules importing each other - as
long as you put references to the "other" module into function or method
bodies, i. e. into code that is not executed during import.

Peter
 
D

Duncan Booth

(e-mail address removed) (sw) wrote in

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?

There are some posts giving solutions to this, but I'm not sure that any of
them really explain the problem.

Remember that import statements and class/def statements, like almost
everything in Python are executed at runtime. Very little (apart from
compiling) happens at compile time.

When an import statement is executed it:

finds the module and compiles or loads it as appropriate.

then, if the module code has already started executing it simply
returns the module.

otherwise, it executes the module code and then returns the module.

So if b is imported first, it imports a, a tries to import b and simply
gets back a reference to b as it currently exists: i.e. without the class
it will later define. If a is imported first then it happens the other way
round, b gets a reference to an incompletely executed module a.

The simplest way to avoid this problem is to remove your 'import b' from
module 'a'. Since in the code you cut this import isn't used it won't be a
problem. If you really have stuff in 'a' that needs to get at 'b' then make
sure that all references from 'a' to 'b' can't execute until after 'b' has
finished importing. References to 'b.something' from inside functions
defined by 'a' will be fine, but you can't refer to 'b.something' from
inside the body (and outside the methods) of ClassA.
 
D

Dennis Lee Bieber

Can you give me some pointers on resolving this circular inclusion? I've got
two classes, in two files:

--a.py--
import b

Delete the above line... Nothing in your sample a.py NEEDS
anything from b.py...


--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top