blob: e9043d2f03c0caa2a6af93483de3b7c45f14fba1 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`modulefinder` --- Find modules used by a script
3=====================================================
4
5.. sectionauthor:: A.M. Kuchling <amk@amk.ca>
6
7
8.. module:: modulefinder
9 :synopsis: Find modules used by a script.
10
11
Georg Brandl116aa622007-08-15 14:28:22 +000012This module provides a :class:`ModuleFinder` class that can be used to determine
13the set of modules imported by a script. ``modulefinder.py`` can also be run as
14a script, giving the filename of a Python script as its argument, after which a
15report of the imported modules will be printed.
16
17
18.. function:: AddPackagePath(pkg_name, path)
19
20 Record that the package named *pkg_name* can be found in the specified *path*.
21
22
23.. function:: ReplacePackage(oldname, newname)
24
25 Allows specifying that the module named *oldname* is in fact the package named
26 *newname*. The most common usage would be to handle how the :mod:`_xmlplus`
27 package replaces the :mod:`xml` package.
28
29
30.. class:: ModuleFinder([path=None, debug=0, excludes=[], replace_paths=[]])
31
32 This class provides :meth:`run_script` and :meth:`report` methods to determine
33 the set of modules imported by a script. *path* can be a list of directories to
34 search for modules; if not specified, ``sys.path`` is used. *debug* sets the
Neal Norwitz752abd02008-05-13 04:55:24 +000035 debugging level; higher values make the class print debugging messages about
Georg Brandl116aa622007-08-15 14:28:22 +000036 what it's doing. *excludes* is a list of module names to exclude from the
37 analysis. *replace_paths* is a list of ``(oldpath, newpath)`` tuples that will
38 be replaced in module paths.
39
40
Benjamin Petersone41251e2008-04-25 01:59:09 +000041 .. method:: report()
Georg Brandl116aa622007-08-15 14:28:22 +000042
Benjamin Petersone41251e2008-04-25 01:59:09 +000043 Print a report to standard output that lists the modules imported by the
44 script and their paths, as well as modules that are missing or seem to be
45 missing.
Georg Brandl116aa622007-08-15 14:28:22 +000046
Benjamin Petersone41251e2008-04-25 01:59:09 +000047 .. method:: run_script(pathname)
Georg Brandl116aa622007-08-15 14:28:22 +000048
Benjamin Petersone41251e2008-04-25 01:59:09 +000049 Analyze the contents of the *pathname* file, which must contain Python
50 code.
Georg Brandl116aa622007-08-15 14:28:22 +000051
Benjamin Petersone41251e2008-04-25 01:59:09 +000052 .. attribute:: modules
Georg Brandl116aa622007-08-15 14:28:22 +000053
Benjamin Petersone41251e2008-04-25 01:59:09 +000054 A dictionary mapping module names to modules. See
55 :ref:`modulefinder-example`
Christian Heimesd3eb5a152008-02-24 00:38:49 +000056
57
58.. _modulefinder-example:
59
60Example usage of :class:`ModuleFinder`
61--------------------------------------
62
63The script that is going to get analyzed later on (bacon.py)::
64
65 import re, itertools
66
67 try:
68 import baconhameggs
69 except ImportError:
70 pass
71
72 try:
73 import guido.python.ham
74 except ImportError:
75 pass
76
77
78The script that will output the report of bacon.py::
79
80 from modulefinder import ModuleFinder
81
82 finder = ModuleFinder()
83 finder.run_script('bacon.py')
84
Neal Norwitz752abd02008-05-13 04:55:24 +000085 print('Loaded modules:')
86 for name, mod in finder.modules.items():
87 print('%s: ' % name, end='')
88 print(','.join(mod.globalnames.keys()[:3]))
Christian Heimesd3eb5a152008-02-24 00:38:49 +000089
Neal Norwitz752abd02008-05-13 04:55:24 +000090 print('-'*50)
91 print('Modules not imported:')
92 print('\n'.join(finder.badmodules.keys()))
Christian Heimesd3eb5a152008-02-24 00:38:49 +000093
94Sample output (may vary depending on the architecture)::
95
96 Loaded modules:
97 _types:
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +000098 copyreg: _inverted_registry,_slotnames,__all__
Christian Heimesd3eb5a152008-02-24 00:38:49 +000099 sre_compile: isstring,_sre,_optimize_unicode
100 _sre:
101 sre_constants: REPEAT_ONE,makedict,AT_END_LINE
102 sys:
103 re: __module__,finditer,_expand
104 itertools:
105 __main__: re,itertools,baconhameggs
106 sre_parse: __getslice__,_PATTERNENDERS,SRE_FLAG_UNICODE
107 array:
108 types: __module__,IntType,TypeType
109 ---------------------------------------------------
110 Modules not imported:
111 guido.python.ham
112 baconhameggs
113
114