namespaces getting confusing

Peter Hansen peter at engcorp.com
Mon Sep 3 13:37:43 EDT 2001


Mark Robinson wrote:
> 
> can anyone explain why I get the following attribute error. I know it is
> something to do with me misusing namespaces and imports but I dunno what.

Circular reference.  You are importing param in Motif and Motif in param,
and Python gets confused.  You have a few options, including refactoring
your code to avoid such tight coupling between different modules, or using
a less procedural style of programming (the circular reference problem
is rarer if you go more object-oriented), or moving one of the
import statements to some place inside a function rather than leaving
them all at the module level.

> %Motif.py
> import param
> 
> def readSeq(filename)
>         #bla bla bla
> 
> #some other def's
> 
> #################
> 
> %param.py
> import os, Motif
> 
> #various variable initialised
> seq = Motif.readSeq
> 

> I get the following error:
> 
> Traceback (most recent call last):
>    File "D:\blobby\param.py", line 1, in ?
>      import os, Motif
>    File "D:\blobby\Motif.py", line 1, in ?
>      import string, math, pickle, param
>    File "D:\blobby\param.py", line 30, in ?
>      seq = Motif.readSeq(filename)
> AttributeError: 'Motif' module has no attribute 'readSeq'

Notice that first param, then Motif, then param again
are being executed.  Note also that you are really not
posting exactly the same code as you ran, since the import
line in Motif.py shows string, math, and pickle, but your
code doesn't.  In this case that probably didn't cause 
any trouble, but in general you should post *exactly* the
code which was run to produce the traceback or you may
get incorrect answers.


-- 
----------------------
Peter Hansen, P.Eng.
peter at engcorp.com



More information about the Python-list mailing list