Python Finding all packages inside a package, even when in an egg
Question: 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.


Answer: [pkgutil](http://docs.python.org/library/pkgutil.html) could be helpfull.

Also see [this SO question.](http://stackoverflow.com/questions/1707709/list-
all-the-modules-that-are-part-of-a-python-package), this is a code example
form [that question](http://stackoverflow.com/questions/1707709/list-all-the-
modules-that-are-part-of-a-python-package).

[kaizer.se](http://stackoverflow.com/questions/1707709/list-all-the-modules-
that-are-part-of-a-python-package/1707786#1707786)

    
    
    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](http://stackoverflow.com/questions/1707709/list-all-the-modules-
that-are-part-of-a-python-package/1708706#1708706)

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

