| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`modulefinder` --- Find modules used by a script | 
 | 2 | ===================================================== | 
 | 3 |  | 
 | 4 | .. sectionauthor:: A.M. Kuchling <amk@amk.ca> | 
 | 5 |  | 
 | 6 |  | 
 | 7 | .. module:: modulefinder | 
 | 8 |    :synopsis: Find modules used by a script. | 
 | 9 |  | 
| Raymond Hettinger | 469271d | 2011-01-27 20:38:46 +0000 | [diff] [blame] | 10 | **Source code:** :source:`Lib/modulefinder.py` | 
 | 11 |  | 
 | 12 | -------------- | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 13 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 14 | This module provides a :class:`ModuleFinder` class that can be used to determine | 
 | 15 | the set of modules imported by a script. ``modulefinder.py`` can also be run as | 
 | 16 | a script, giving the filename of a Python script as its argument, after which a | 
 | 17 | report of the imported modules will be printed. | 
 | 18 |  | 
 | 19 |  | 
 | 20 | .. function:: AddPackagePath(pkg_name, path) | 
 | 21 |  | 
 | 22 |    Record that the package named *pkg_name* can be found in the specified *path*. | 
 | 23 |  | 
 | 24 |  | 
 | 25 | .. function:: ReplacePackage(oldname, newname) | 
 | 26 |  | 
 | 27 |    Allows specifying that the module named *oldname* is in fact the package named | 
| Martin v. Löwis | 2f48d89 | 2011-05-09 08:05:43 +0200 | [diff] [blame] | 28 |    *newname*. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 29 |  | 
 | 30 |  | 
| Georg Brandl | cd7f32b | 2009-06-08 09:13:45 +0000 | [diff] [blame] | 31 | .. class:: ModuleFinder(path=None, debug=0, excludes=[], replace_paths=[]) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 32 |  | 
 | 33 |    This class provides :meth:`run_script` and :meth:`report` methods to determine | 
 | 34 |    the set of modules imported by a script. *path* can be a list of directories to | 
 | 35 |    search for modules; if not specified, ``sys.path`` is used.  *debug* sets the | 
| Neal Norwitz | 752abd0 | 2008-05-13 04:55:24 +0000 | [diff] [blame] | 36 |    debugging level; higher values make the class print debugging messages about | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 37 |    what it's doing. *excludes* is a list of module names to exclude from the | 
 | 38 |    analysis. *replace_paths* is a list of ``(oldpath, newpath)`` tuples that will | 
 | 39 |    be replaced in module paths. | 
 | 40 |  | 
 | 41 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 42 |    .. method:: report() | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 43 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 44 |       Print a report to standard output that lists the modules imported by the | 
 | 45 |       script and their paths, as well as modules that are missing or seem to be | 
 | 46 |       missing. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 47 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 48 |    .. method:: run_script(pathname) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 49 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 50 |       Analyze the contents of the *pathname* file, which must contain Python | 
 | 51 |       code. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 52 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 53 |    .. attribute:: modules | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 54 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 55 |       A dictionary mapping module names to modules. See | 
 | 56 |       :ref:`modulefinder-example` | 
| Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 57 |  | 
 | 58 |  | 
 | 59 | .. _modulefinder-example: | 
 | 60 |  | 
 | 61 | Example usage of :class:`ModuleFinder` | 
 | 62 | -------------------------------------- | 
 | 63 |  | 
 | 64 | The script that is going to get analyzed later on (bacon.py):: | 
 | 65 |  | 
 | 66 |    import re, itertools | 
 | 67 |  | 
 | 68 |    try: | 
 | 69 |        import baconhameggs | 
 | 70 |    except ImportError: | 
 | 71 |        pass | 
 | 72 |  | 
 | 73 |    try: | 
 | 74 |        import guido.python.ham | 
 | 75 |    except ImportError: | 
 | 76 |        pass | 
 | 77 |  | 
 | 78 |  | 
 | 79 | The script that will output the report of bacon.py:: | 
 | 80 |  | 
 | 81 |    from modulefinder import ModuleFinder | 
 | 82 |  | 
 | 83 |    finder = ModuleFinder() | 
 | 84 |    finder.run_script('bacon.py') | 
 | 85 |  | 
| Neal Norwitz | 752abd0 | 2008-05-13 04:55:24 +0000 | [diff] [blame] | 86 |    print('Loaded modules:') | 
 | 87 |    for name, mod in finder.modules.items(): | 
 | 88 |        print('%s: ' % name, end='') | 
| Ezio Melotti | 8f7649e | 2009-09-13 04:48:45 +0000 | [diff] [blame] | 89 |        print(','.join(list(mod.globalnames.keys())[:3])) | 
| Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 90 |  | 
| Neal Norwitz | 752abd0 | 2008-05-13 04:55:24 +0000 | [diff] [blame] | 91 |    print('-'*50) | 
 | 92 |    print('Modules not imported:') | 
 | 93 |    print('\n'.join(finder.badmodules.keys())) | 
| Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 94 |  | 
 | 95 | Sample output (may vary depending on the architecture):: | 
 | 96 |  | 
 | 97 |     Loaded modules: | 
 | 98 |     _types: | 
| Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 99 |     copyreg:  _inverted_registry,_slotnames,__all__ | 
| Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 100 |     sre_compile:  isstring,_sre,_optimize_unicode | 
 | 101 |     _sre: | 
 | 102 |     sre_constants:  REPEAT_ONE,makedict,AT_END_LINE | 
 | 103 |     sys: | 
 | 104 |     re:  __module__,finditer,_expand | 
 | 105 |     itertools: | 
 | 106 |     __main__:  re,itertools,baconhameggs | 
 | 107 |     sre_parse:  __getslice__,_PATTERNENDERS,SRE_FLAG_UNICODE | 
 | 108 |     array: | 
 | 109 |     types:  __module__,IntType,TypeType | 
 | 110 |     --------------------------------------------------- | 
 | 111 |     Modules not imported: | 
 | 112 |     guido.python.ham | 
 | 113 |     baconhameggs | 
 | 114 |  | 
 | 115 |  |