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

Peter Otten __peter__ at web.de
Thu Aug 26 09:29:33 EDT 2010


Chris Withers wrote:

>  From the docs of pkgutils.walk_packages:
> """
>      'onerror' is a function which gets called with one argument (the
>      name of the package which was being imported) if any exception
>      occurs while trying to import a package.  If no onerror function is
>      supplied, ImportErrors are caught and ignored, while all other
>      exceptions are propagated, terminating the search.
> """
> 
> 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.
 
> However, because the yield is before the import check, you can get
> packages returned that are not importable.
> 
> This feels at odds with the docs above and I think is a bug.
> 
> If the yield were dropped to befoer the import check, we wouldn't have
> this problem.
> 
> what do others think?

I've never worked with that function; I'd like to hear more about your 
usecase.

Peter



More information about the Python-list mailing list