How can I list all packages/modules available to Python from within a Python script?
Solution 1:
Alright, I was curious, and I digged a bit into pkgutil
, and I came up with this, which is much simpler than I expected:
list(pkgutil.iter_modules())
It lists all top-level packages/modules available either as regular files or zip packages, without loading them. It will not see other types of packages though, unless they properly register with the pkgutil
internals.
Each returned entry is a 3-tuple with the items:
-
module_finder
: The file finder instance that found the module -
name
: The name of the module -
ispkg
: A boolean specifying whether it is a regular module or a package.
Example 3-tuple:
(FileFinder('/usr/lib/python3/dist-packages'), 'PIL', True)
And I can confirm that this did not load the PIL package:
In [11]: sys.modules['PIL']
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-11-b0fc0af6cc34> in <module>()
----> 1 sys.modules['PIL']
KeyError: 'PIL'
Solution 2:
I put together a very rough way of getting this list (see below), which appears to be more accurate than pkgutil
. See details below.
In addition, I found loaded_modules and list-imports, but I tested none of them.
I have compared the results of my method with the answer by spectras:
- All items in the output by spectras (say,
modlist2
) are in the output here (say,modlist1
). - There are quite a few items in
modlist1
that are not inmodlist2
. To my surprise, this difference included modules likesys
,math
,zlib
, etc. In my case, the respective lengths were 390 vs. 327, so the method withpkgutil
gives quite incomplete results.
The method to pull the list of available modules consists of:
- Capturing output of
help
into a string - Removing spare text from the captured string
- Splitting multicolumn output
Code is here:
def modules_list() :
"""Return a list of available modules"""
import sys
# Capture output of help into a string
import io
stdout_sys = sys.stdout
stdout_capture = io.StringIO()
sys.stdout = stdout_capture
help('modules')
sys.stdout = stdout_sys
help_out = stdout_capture.getvalue()
# Remove extra text from string
help_out = help_out.replace('.', '')
help_out = help_out.replace('available modules', '%').replace('Enter any module', '%').split('%')[-2]
# Split multicolumn output
help_out = help_out.replace('\n', '%').replace(' ', '%').split('%')
help_out = list(filter(None, help_out))
help_out.sort()
return help_out