blob: d42c6ab78370c1425b42ff4f5e904299b22a0ac6 [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
Raymond Hettinger469271d2011-01-27 20:38:46 +000010**Source code:** :source:`Lib/modulefinder.py`
11
12--------------
Georg Brandl116aa622007-08-15 14:28:22 +000013
Georg Brandl116aa622007-08-15 14:28:22 +000014This module provides a :class:`ModuleFinder` class that can be used to determine
15the set of modules imported by a script. ``modulefinder.py`` can also be run as
16a script, giving the filename of a Python script as its argument, after which a
17report 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
28 *newname*. The most common usage would be to handle how the :mod:`_xmlplus`
29 package replaces the :mod:`xml` package.
30
31
Georg Brandlcd7f32b2009-06-08 09:13:45 +000032.. class:: ModuleFinder(path=None, debug=0, excludes=[], replace_paths=[])
Georg Brandl116aa622007-08-15 14:28:22 +000033
34 This class provides :meth:`run_script` and :meth:`report` methods to determine
35 the set of modules imported by a script. *path* can be a list of directories to
36 search for modules; if not specified, ``sys.path`` is used. *debug* sets the
Neal Norwitz752abd02008-05-13 04:55:24 +000037 debugging level; higher values make the class print debugging messages about
Georg Brandl116aa622007-08-15 14:28:22 +000038 what it's doing. *excludes* is a list of module names to exclude from the
39 analysis. *replace_paths* is a list of ``(oldpath, newpath)`` tuples that will
40 be replaced in module paths.
41
42
Benjamin Petersone41251e2008-04-25 01:59:09 +000043 .. method:: report()
Georg Brandl116aa622007-08-15 14:28:22 +000044
Benjamin Petersone41251e2008-04-25 01:59:09 +000045 Print a report to standard output that lists the modules imported by the
46 script and their paths, as well as modules that are missing or seem to be
47 missing.
Georg Brandl116aa622007-08-15 14:28:22 +000048
Benjamin Petersone41251e2008-04-25 01:59:09 +000049 .. method:: run_script(pathname)
Georg Brandl116aa622007-08-15 14:28:22 +000050
Benjamin Petersone41251e2008-04-25 01:59:09 +000051 Analyze the contents of the *pathname* file, which must contain Python
52 code.
Georg Brandl116aa622007-08-15 14:28:22 +000053
Benjamin Petersone41251e2008-04-25 01:59:09 +000054 .. attribute:: modules
Georg Brandl116aa622007-08-15 14:28:22 +000055
Benjamin Petersone41251e2008-04-25 01:59:09 +000056 A dictionary mapping module names to modules. See
57 :ref:`modulefinder-example`
Christian Heimesd3eb5a152008-02-24 00:38:49 +000058
59
60.. _modulefinder-example:
61
62Example usage of :class:`ModuleFinder`
63--------------------------------------
64
65The script that is going to get analyzed later on (bacon.py)::
66
67 import re, itertools
68
69 try:
70 import baconhameggs
71 except ImportError:
72 pass
73
74 try:
75 import guido.python.ham
76 except ImportError:
77 pass
78
79
80The script that will output the report of bacon.py::
81
82 from modulefinder import ModuleFinder
83
84 finder = ModuleFinder()
85 finder.run_script('bacon.py')
86
Neal Norwitz752abd02008-05-13 04:55:24 +000087 print('Loaded modules:')
88 for name, mod in finder.modules.items():
89 print('%s: ' % name, end='')
Ezio Melotti8f7649e2009-09-13 04:48:45 +000090 print(','.join(list(mod.globalnames.keys())[:3]))
Christian Heimesd3eb5a152008-02-24 00:38:49 +000091
Neal Norwitz752abd02008-05-13 04:55:24 +000092 print('-'*50)
93 print('Modules not imported:')
94 print('\n'.join(finder.badmodules.keys()))
Christian Heimesd3eb5a152008-02-24 00:38:49 +000095
96Sample output (may vary depending on the architecture)::
97
98 Loaded modules:
99 _types:
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +0000100 copyreg: _inverted_registry,_slotnames,__all__
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000101 sre_compile: isstring,_sre,_optimize_unicode
102 _sre:
103 sre_constants: REPEAT_ONE,makedict,AT_END_LINE
104 sys:
105 re: __module__,finditer,_expand
106 itertools:
107 __main__: re,itertools,baconhameggs
108 sre_parse: __getslice__,_PATTERNENDERS,SRE_FLAG_UNICODE
109 array:
110 types: __module__,IntType,TypeType
111 ---------------------------------------------------
112 Modules not imported:
113 guido.python.ham
114 baconhameggs
115
116