bug? pkgutil.walk_packages returns packages that result in an ImportError

Peter Otten __peter__ at web.de
Mon Aug 30 22:39:32 CEST 2010

Chris Withers wrote:

> Peter Otten wrote:
>>> My expectation of this is that if onerrors is left as None, names
>>> yielded will be importable.
>> I would infer no such promise, especially as the generator also yields
>> modules, and no attempt at all is made to import those.
> Really? 

Yes, and the docstring gives a hint:

Note that this function must import all *packages* (NOT all
modules!) on the given path, in order to access the __path__
attribute to find submodules.
""" Emphasis *not* mine ;)

> I thought the __import__ fired over everything?

If in doubt have a look at the source code or make a little testcase.

> It's becoming a common use case for packages like martian [1] and
> venutian [2] that need to scan whole packages for things. In my case,
> I'm scanning for declaratively mapped SQLAlchemy models.
> It would be great if this function became part of the public API and got
> some more definition on what it will and won't do. When should I take
> this to Python-Dev?

My impression is that it was a conscious decision to import as little as 
necessary. It is easy to import the modules once you have their names, but 
impossible to undo an unwanted import.

If you think the approach you expected is superior you can also escalate to 
python-ideas and see if you get more feedback there or make a feature 
request on the bug tracker.


More information about the Python-list mailing list