subpackage import problem

Robert Kern rkern at
Sun Aug 21 09:36:43 CEST 2005

Eric Huss wrote:
> I'm having a problem with packages within packages.  Here's an example:
> foo/
> foo/ empty file
> foo/sub/
> 	from foo.sub.B import B
> foo/sub/
> 	class A:
> 		pass
> foo/sub/
> 	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/", line 1, in ?
>     from foo.sub.B import B
>   File "foo/sub/", 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/ while it's trying to create the
module object foo.sub. You can get around it, though.

Try this in foo/sub/ :

  from foo.sub.A import A
  class B(A):

That works for me (Python 2.4.1, OS X).

Robert Kern
rkern at

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter

More information about the Python-list mailing list