creating a list of all imported modules

Duncan Booth duncan.booth at invalid.invalid
Tue Mar 10 09:56:47 CET 2009


mataap at gmail.com wrote:

>>>> for i in dir() :
> ...   t = eval( 'type(' + i + ')' )
> ...   if re.match('.*module.*',str(t)) : print i, t
> ...

If you think you need eval stop and think again. You don't.
If you think you need regular expressions stop and think again. You usually 
don't.

>>> def imported(namespace=None):
	from types import ModuleType
	if namespace is None:
		namespace = globals()
	for n, v in namespace.iteritems():
		if isinstance(v, ModuleType):
			print n

			
>>> imported()
re
__builtins__
sys
os
types

-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list