modulefinder.rst 3.16 KB
Newer Older
1 2 3 4 5 6
:mod:`modulefinder` --- Find modules used by a script
=====================================================

.. module:: modulefinder
   :synopsis: Find modules used by a script.

7 8
.. sectionauthor:: A.M. Kuchling <amk@amk.ca>

Raymond Hettinger's avatar
Raymond Hettinger committed
9 10 11
**Source code:** :source:`Lib/modulefinder.py`

--------------
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

This module provides a :class:`ModuleFinder` class that can be used to determine
the set of modules imported by a script. ``modulefinder.py`` can also be run as
a script, giving the filename of a Python script as its argument, after which a
report of the imported modules will be printed.


.. function:: AddPackagePath(pkg_name, path)

   Record that the package named *pkg_name* can be found in the specified *path*.


.. function:: ReplacePackage(oldname, newname)

   Allows specifying that the module named *oldname* is in fact the package named
27
   *newname*.
28 29


30
.. class:: ModuleFinder(path=None, debug=0, excludes=[], replace_paths=[])
31 32 33 34

   This class provides :meth:`run_script` and :meth:`report` methods to determine
   the set of modules imported by a script. *path* can be a list of directories to
   search for modules; if not specified, ``sys.path`` is used.  *debug* sets the
35
   debugging level; higher values make the class print debugging messages about
36 37 38 39 40
   what it's doing. *excludes* is a list of module names to exclude from the
   analysis. *replace_paths* is a list of ``(oldpath, newpath)`` tuples that will
   be replaced in module paths.


41
   .. method:: report()
42

43 44 45
      Print a report to standard output that lists the modules imported by the
      script and their paths, as well as modules that are missing or seem to be
      missing.
46

47
   .. method:: run_script(pathname)
48

49 50
      Analyze the contents of the *pathname* file, which must contain Python
      code.
51

52
   .. attribute:: modules
53

54
      A dictionary mapping module names to modules. See
55
      :ref:`modulefinder-example`.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84


.. _modulefinder-example:

Example usage of :class:`ModuleFinder`
--------------------------------------

The script that is going to get analyzed later on (bacon.py)::

   import re, itertools

   try:
       import baconhameggs
   except ImportError:
       pass

   try:
       import guido.python.ham
   except ImportError:
       pass


The script that will output the report of bacon.py::

   from modulefinder import ModuleFinder

   finder = ModuleFinder()
   finder.run_script('bacon.py')

85 86 87
   print('Loaded modules:')
   for name, mod in finder.modules.items():
       print('%s: ' % name, end='')
Ezio Melotti's avatar
Ezio Melotti committed
88
       print(','.join(list(mod.globalnames.keys())[:3]))
89

90 91 92
   print('-'*50)
   print('Modules not imported:')
   print('\n'.join(finder.badmodules.keys()))
93 94 95 96 97

Sample output (may vary depending on the architecture)::

    Loaded modules:
    _types:
98
    copyreg:  _inverted_registry,_slotnames,__all__
99 100 101 102 103 104 105
    sre_compile:  isstring,_sre,_optimize_unicode
    _sre:
    sre_constants:  REPEAT_ONE,makedict,AT_END_LINE
    sys:
    re:  __module__,finditer,_expand
    itertools:
    __main__:  re,itertools,baconhameggs
106
    sre_parse:  _PATTERNENDERS,SRE_FLAG_UNICODE
107 108 109 110 111 112 113 114
    array:
    types:  __module__,IntType,TypeType
    ---------------------------------------------------
    Modules not imported:
    guido.python.ham
    baconhameggs