Getting a list of all modules

Ian Kelly ian.g.kelly at gmail.com
Wed Jul 30 16:29:04 CEST 2014


On Jul 30, 2014 4:37 AM, "Robert Kern" <robert.kern at gmail.com> wrote:
>
> On 2014-07-30 09:46, Peter Otten wrote:
>>
>> Steven D'Aprano wrote:
>>
>>> I'm looking for a programmatic way to get a list of all Python modules
>>> and packages. Not just those already imported, but all those which
>>> *could* be imported.
>>>
>>> I have a quick-and-dirty function which half does the job:
>>>
>>>
>>> def get_modules():
>>>      extensions = ('.py', '.pyc', '.pyo', '.so', '.dll')
>>>      matches = set()
>>>      for location in sys.path:
>>>          if location == '': location = '.'
>>>          if os.path.isdir(location):
>>>              for name in os.listdir(location):
>>>                  base, ext = os.path.splitext(name)
>>>                  if ext in extensions:
>>>                      matches.add(base)
>>>      return sorted(matches)
>>>
>>>
>>>
>>> but I know it's wrong (it doesn't handle packages correctly, or zip
>>> files, doesn't follow .pth files, has a very naive understanding of
cross-
>>> platform issues, fails to include built-in modules that don't live in
the
>>> file system, and probably more).
>>>
>>> Is this problem already solved? Can anyone make any suggestions?
>>
>>
>> $ python3 -m pydoc -b
>>
>> shows a page with modules that I think is more complete than what you
have.
>> A quick glance at the implementation suggests that the hard work is done
by
>>
>> pkgutil.iter_modules()
>
>
> There are two niggles to this answer: it omits builtin modules, but those
are easily discovered through sys.builtin_module_names. It can also include
spurious script .py files that cannot be imported because their names are
not Python identifiers: e.g. check-newconfigs.py. Those are easy to filter
out, fortunately.

It will also omit any modules provided by a custom module finder that
doesn't implement iter_modules, which is not a required part of the
interface.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20140730/cced71fa/attachment.html>


More information about the Python-list mailing list