3

Given a Python package, how can I automatically find all its sub-packages?

I used to have a function that would just browse the file system, looking for folders that have an __init__.py* file in them, but now I need a method that would work even if the whole package is in an egg.

Ram Rachum
  • 84,019
  • 84
  • 236
  • 374

1 Answers1

0

pkgutil could be helpfull.

Also see this SO question., this is a code example form that question.

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)
Community
  • 1
  • 1
Davor Lucic
  • 28,970
  • 8
  • 66
  • 76