subpackage import problem

Eric Huss e-huss at netmeridian.com
Thu Aug 25 04:51:31 CEST 2005


On Sun, 21 Aug 2005, Robert Kern wrote:

> Eric Huss wrote:
> > I'm having a problem with packages within packages.  Here's an example:
> >
> > foo/
> > foo/__init__.py: empty file
> > foo/sub/__init__.py:
> > 	from foo.sub.B import B
> > foo/sub/A.py:
> > 	class A:
> > 		pass
> > foo/sub/B.py
> > 	import foo.sub.A
> > 	class B(foo.sub.A):
> > 		pass
> >
> > Trying to "import foo.sub" will result in this error:
> >
> >>>>import foo.sub
> >
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in ?
> >   File "foo/sub/__init__.py", line 1, in ?
> >     from foo.sub.B import B
> >   File "foo/sub/B.py", line 3, in ?
> >     class B(foo.sub.A):
> > AttributeError: 'module' object has no attribute 'sub'
>
> I imagine it has something to do with the timing of imports. Python is
> executing the contents of foo/sub/B.py while it's trying to create the
> module object foo.sub. You can get around it, though.
>
> Try this in foo/sub/B.py :
>
>   from foo.sub.A import A
>   class B(A):
>       pass
>
> That works for me (Python 2.4.1, OS X).

Hi!  I wanted to say thank you for responding.  Your suggestion works for
me.  After sending my message I found some related bugs on sourceforge
(http://sourceforge.net/tracker/?func=detail&aid=992389&group_id=5470&atid=105470
and
http://sourceforge.net/tracker/?func=detail&aid=966431&group_id=5470&atid=105470).

It's unfortunate that it doesn't work, and it sounds like the fix would
require a major amount of work in import.c, which means it will probably
never be fixed (and if I made a big patch, I doubt it would be committed).

-Eric



More information about the Python-list mailing list