Nested packages and import order bug

Ian Bicking ianb at colorstudy.com
Mon Oct 6 13:58:44 EDT 2003


On Monday, October 6, 2003, at 11:53 AM, Joerg Sonnenberger wrote:
> Hi all,
> I have a package a.b with the following content:
>
> a/b/__init__.py:
>   import a.b
>   dir(a.b)
>
> Running "import a.b" generates an AttributeError for b, obviously
> the import didn't add b to the module "a". Even though it can be
> argued that importing a package from within is bad style, this
> a clearly a bug since its at least surprising.

This sort of bug is more common with circular imports, but in this case 
the circle is very small (the module importing itself).  The module 
isn't "imported" until all its statements have been executed.  When you 
consider the statement execution order it's usually clear why it can't 
work.

> Shouldn't the import create the namespace entry in a after it
> created the module entry in sys.modules?
>
> Therefore should I submit a bug report?

This is not an entirely uncommon error -- or maybe it's more that when 
it does occur it usually confuses people.

I don't think the behavior is a bug.  Maybe it could have a better 
error message, but I'm not entirely sure how you'd even detect this 
particular case.

   Ian






More information about the Python-list mailing list