The simplest way I can think of is using the 'inspect' module (comes in the standard Python library since version 2.1, here's the doc:
).
For example, to list all modules in the Django namespace you could use code like this:
[('conf',
<module 'django.conf' from '/Users/david/.virtualenvs/backend/lib/python2.7/site-packages/django/conf/__init__.pyc'>),
('contrib',
<module 'django.contrib' from '/Users/david/.virtualenvs/backend/lib/python2.7/site-packages/django/contrib/__init__.pyc'>),
('core',
<module 'django.core' from '/Users/david/.virtualenvs/backend/lib/python2.7/site-packages/django/core/__init__.pyc'>),
('db',
<module 'django.db' from '/Users/david/.virtualenvs/backend/lib/python2.7/site-packages/django/db/__init__.pyc'>),
('dispatch',
<module 'django.dispatch' from '/Users/david/.virtualenvs/backend/lib/python2.7/site-packages/django/dispatch/__init__.pyc'>),
('forms',
<module 'django.forms' from '/Users/david/.virtualenvs/backend/lib/python2.7/site-packages/django/forms/__init__.pyc'>),
('http',
<module 'django.http' from '/Users/david/.virtualenvs/backend/lib/python2.7/site-packages/django/http/__init__.pyc'>),
('middleware',
<module 'django.middleware' from '/Users/david/.virtualenvs/backend/lib/python2.7/site-packages/django/middleware/__init__.pyc'>),
('shortcuts',
<module 'django.shortcuts' from '/Users/david/.virtualenvs/backend/lib/python2.7/site-packages/django/shortcuts/__init__.pyc'>),
('template',
<module 'django.template' from '/Users/david/.virtualenvs/backend/lib/python2.7/site-packages/django/template/__init__.pyc'>),
('test',
<module 'django.test' from '/Users/david/.virtualenvs/backend/lib/python2.7/site-packages/django/test/__init__.pyc'>),
('utils',
<module 'django.utils' from '/Users/david/.virtualenvs/backend/lib/python2.7/site-packages/django/utils/__init__.pyc'>),
('views',
<module 'django.views' from '/Users/david/.virtualenvs/backend/lib/python2.7/site-packages/django/views/__init__.pyc'>)]
Please notices these are tuples where the first element is the name of the module and the second element is the module itself. Simply by recursing on the second element of the tuple you can generate a full list of modules for the namespace (left as an exercise to the reader :)).
Cheers,
David