blob: 41b387bb993939f871523e78e08a9c595130854d [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001: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 Brandl116aa622007-08-15 14:28:22 +000011This module provides a :class:`ModuleFinder` class that can be used to determine
12the set of modules imported by a script. ``modulefinder.py`` can also be run as
13a script, giving the filename of a Python script as its argument, after which a
14report 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 Brandlcd7f32b2009-06-08 09:13:45 +000029.. class:: ModuleFinder(path=None, debug=0, excludes=[], replace_paths=[])
Georg Brandl116aa622007-08-15 14:28:22 +000030
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 Norwitz752abd02008-05-13 04:55:24 +000034 debugging level; higher values make the class print debugging messages about
Georg Brandl116aa622007-08-15 14:28:22 +000035 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 Petersone41251e2008-04-25 01:59:09 +000040 .. method:: report()
Georg Brandl116aa622007-08-15 14:28:22 +000041
Benjamin Petersone41251e2008-04-25 01:59:09 +000042 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 Brandl116aa622007-08-15 14:28:22 +000045
Benjamin Petersone41251e2008-04-25 01:59:09 +000046 .. method:: run_script(pathname)
Georg Brandl116aa622007-08-15 14:28:22 +000047
Benjamin Petersone41251e2008-04-25 01:59:09 +000048 Analyze the contents of the *pathname* file, which must contain Python
49 code.
Georg Brandl116aa622007-08-15 14:28:22 +000050
Benjamin Petersone41251e2008-04-25 01:59:09 +000051 .. attribute:: modules
Georg Brandl116aa622007-08-15 14:28:22 +000052
Benjamin Petersone41251e2008-04-25 01:59:09 +000053 A dictionary mapping module names to modules. See
54 :ref:`modulefinder-example`
Christian Heimesd3eb5a152008-02-24 00:38:49 +000055
56
57.. _modulefinder-example:
58
59Example usage of :class:`ModuleFinder`
60--------------------------------------
61
62The 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
77The 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 Norwitz752abd02008-05-13 04:55:24 +000084 print('Loaded modules:')
85 for name, mod in finder.modules.items():
86 print('%s: ' % name, end='')
Ezio Melotti8f7649e2009-09-13 04:48:45 +000087 print(','.join(list(mod.globalnames.keys())[:3]))
Christian Heimesd3eb5a152008-02-24 00:38:49 +000088
Neal Norwitz752abd02008-05-13 04:55:24 +000089 print('-'*50)
90 print('Modules not imported:')
91 print('\n'.join(finder.badmodules.keys()))
Christian Heimesd3eb5a152008-02-24 00:38:49 +000092
93Sample output (may vary depending on the architecture)::
94
95 Loaded modules:
96 _types:
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +000097 copyreg: _inverted_registry,_slotnames,__all__
Christian Heimesd3eb5a152008-02-24 00:38:49 +000098 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