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