How can I list all packages/modules available to Python from within a Python script?
<p>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.</p>
<p>I looked at these:</p>
<ul>
<li><a href="https://stackoverflow.com/questions/1707709/list-all-the-modules-that-are-part-of-a-python-package">List all the modules that are part of a python package?</a></li>
<li><a href="https://stackoverflow.com/questions/5747598/python-package-structure">Python package structure</a></li>
<li><a href="https://bytes.com/topic/python/answers/36924-how-get-list-modules-package" rel="nofollow noreferrer">How to: get list of modules in a package</a></li>
</ul>
<p>But they are not what I am looking for.</p>
<p>I attempted to do this:</p>
<pre><code>import pkgutil
for pkg in pkgutil.walk_packages():
    print(pkg)  # or do something with them...
</code></pre>
<p>However, when I do <a href="https://stackoverflow.com/a/4858123/6030424">this</a>:</p>
<pre><code>import sys
sys.modules.keys()​​​
</code></pre>
<p>It appears that I have loaded all the packages which is <strong>not</strong> 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.</p>