Python Search for all packages inside a package, even if in an egg

Given a Python package, how can I automatically find all its subpackages?

I had a function that would just scan the file system, looking for folders in which there is a file __init__.py*, but now I need a method that will work even if the whole package is in an egg.

+3
source share
1 answer

pkgutil may be helpful.

Also see this SO question. This is a form of sample code.

kaizer.se

import pkgutil
# this is the package we are inspecting -- for example 'email' from stdlib
import email
package = email
for importer, modname, ispkg in pkgutil.iter_modules(package.__path__):
    print "Found submodule %s (is a package: %s)" % (modname, ispkg)

~ unutbu

import pkgutil
for importer, modname, ispkg in pkgutil.walk_packages(path=None, onerror=lambda x: None):
    print(modname)
0
source

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


All Articles