Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: List of Modules (>>> help('modules') not working)

I wanted a list of my modules and was told:

>>> help('modules')

Would do the trick. But I just get

Please wait a moment while I gather a list of all available modules...

For over 10 minutes before I killed it.

Anyone know what could be causing this? Or how I could otherwise see my modules? (System Ubuntu 9.10/Python 2.6.4)

Thanks,

Dan

like image 928
Dan Avatar asked Dec 28 '22 21:12

Dan


2 Answers

help("modules") can take a long time, because it has to import each module before it can search that module's path for sub-modules. This can be a problem if any module has code outside of an if __name__ == "__main__": guard, and if that code expects user input or enters an infinite loop or hangs for any other reason.

Under the hood, help("modules") calls pkgutil.walk_packages, which exhibits the aforementioned "import everything" behavior. As an alternative, you can call iter_modules, which does not import everything, with the drawback of only iterating top-level modules.

>>> import pkgutil
>>> print [tup[1] for tup in pkgutil.iter_modules()]
['colorama', 'xlrd', 'BeautifulSoup', 'BeautifulSoupTests', '_ctypes', ...
#snip... 
..., 'pywin', 'win32ui', 'win32uiole']

This will also miss out on some built-in modules, which you can get separately using sys.

>>> import sys
>>> sys.builtin_module_names
('__builtin__', '__main__', '_ast', '_bisect', '_codecs', ...
#snip...
..., 'thread', 'time', 'xxsubtype', 'zipimport', 'zlib')
like image 173
Kevin Avatar answered Dec 31 '22 10:12

Kevin


If you want to see the modules you have imported (directly or indirectly),

>>> import sys
>>> print sys.modules

help('modules') is about all modules that are available -- i.e. ones you **could* import if you wished. It doesn't take anywhere as long for me as it does for you, but if you have installed enough extensions it could have thousands, or tens of thousands, "potential" modules to show, so it's not surprising that it might take a bit of time gathering that info.

like image 24
Alex Martelli Avatar answered Dec 31 '22 11:12

Alex Martelli