I have looked around and do not see a solution for this. What I would like to do is get a list of all packages available in Python at run-time.
I looked at these:
But they are not what I am looking for.
I attempted to do this:
import pkgutil
for pkg in pkgutil.walk_packages():
print(pkg) # or do something with them...
However, when I do this:
import sys
sys.modules.keys()
It appears that I have loaded all the packages which is not what I want to do, what I want is a list of strings of all packages+modules available to the current Python installation without loading them all when I do it.
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 modulename
: The name of the moduleispkg
: 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'
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:
modlist2
) are in the output here (say, modlist1
).modlist1
that are not in modlist2
.
To my surprise, this difference included modules like sys
, math
, zlib
, etc.
In my case, the respective lengths were 390 vs. 327, so the method with pkgutil
gives quite incomplete results.The method to pull the list of available modules consists of:
help
into a stringCode 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With