List of modules in a namespace package

I am trying to get Python to list all the modules in a namespace package.

I have the following file structure:

cwd
|--a
|  `--ns
|     |--__init__.py
|     `--amod.py
|--b
|  `--ns
|     |--__init__.py
|     `--bmod.py
`--c
   `--ns
      |--__init__.py
      `--cmod.py

Each __init__.pydefines this package as a namespace package, having the following line:

__import__('pkg_resources').declare_namespace(__name__)

The module amodcontains a class A, bmodcontains another class, Band cmodcontains a class C.

With a clean environment, the following occurs:

>>> import inspect, sys, glob
>>> sys.path += glob.glob('*')
>>> import ns
>>> inspect.getmembers(ns, inspect.ismodule)
[]

As you can see, the modules are not specified.

Now, when I import the modules manually, then call the check again:

>>> inspect.getmembers(ns, inspect.ismodule)
[('amod', <module 'ns.amod' from 'a/ns/amod.pyc'>), ('bmod', <module 'ns.bmod' from 'b/ns/bmod.pyc'>), ('cmod', <module 'ns.cmod' from 'c/ns/cmod.pyc'>)]

Now I would like the call to inspectwork without importing the modules manually, so how can I do this?

+5
source share

All Articles