blob: d39f4123d8ed345dafd3c415237ee1d1f7087506 [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
35 debugging level; higher values make the class print debugging messages about
36 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
41.. method:: ModuleFinder.report()
42
43 Print a report to standard output that lists the modules imported by the script
44 and their paths, as well as modules that are missing or seem to be missing.
45
46
47.. method:: ModuleFinder.run_script(pathname)
48
49 Analyze the contents of the *pathname* file, which must contain Python code.
50
Christian Heimesd3eb5a152008-02-24 00:38:49 +000051.. attribute:: ModuleFinder.modules
52
53 A dictionary mapping module names to modules. See :ref:`modulefinder-example`
54
55
56.. _modulefinder-example:
57
58Example usage of :class:`ModuleFinder`
59--------------------------------------
60
61The script that is going to get analyzed later on (bacon.py)::
62
63 import re, itertools
64
65 try:
66 import baconhameggs
67 except ImportError:
68 pass
69
70 try:
71 import guido.python.ham
72 except ImportError:
73 pass
74
75
76The script that will output the report of bacon.py::
77
78 from modulefinder import ModuleFinder
79
80 finder = ModuleFinder()
81 finder.run_script('bacon.py')
82
83 print 'Loaded modules:'
84 for name, mod in finder.modules.iteritems():
85 print '%s: ' % name,
86 print ','.join(mod.globalnames.keys()[:3])
87
88 print '-'*50
89 print 'Modules not imported:'
90 print '\n'.join(finder.badmodules.iterkeys())
91
92Sample output (may vary depending on the architecture)::
93
94 Loaded modules:
95 _types:
96 copy_reg: _inverted_registry,_slotnames,__all__
97 sre_compile: isstring,_sre,_optimize_unicode
98 _sre:
99 sre_constants: REPEAT_ONE,makedict,AT_END_LINE
100 sys:
101 re: __module__,finditer,_expand
102 itertools:
103 __main__: re,itertools,baconhameggs
104 sre_parse: __getslice__,_PATTERNENDERS,SRE_FLAG_UNICODE
105 array:
106 types: __module__,IntType,TypeType
107 ---------------------------------------------------
108 Modules not imported:
109 guido.python.ham
110 baconhameggs
111
112