Python pkgutil.walk_packages does not return subpackages

I have a package layout:

scenarios/ __init__.py X/ __init__.py Y/ __init__.py Z/ __init__.py 

I performed

 import scenarios pkgutil.walk_packages(scenarios.__path__, scenarios.__name__ + '.') 

But this generates a list including only packages X and Z, Y is missing. What can I use to get all subdirectories?

thanks

+6
source share
1 answer

Here is the theory: the walk_packages function tries to import each specified module. When it falls into subpacket "Y", it tries to import it, but there is an error. By default, this error is suppressed. A side effect is that the walk_packages function does not regress in Y. You can test this theory using the keyword argument "onerror". For instance:

 import sys, pkgutil from traceback import print_tb def onerror(name): print("Error importing module %s" % name) type, value, traceback = sys.exc_info() print_tb(traceback) import scenarios pkgutil.walk_packages(scenarios.__path__, scenarios.__name__ + '.', onerror=onerror) 
+5
source

Source: https://habr.com/ru/post/946955/


All Articles