blob: 41c603ce9bdb9a67cf8f10a6602b61ff086c68c2 [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
Martin v. Löwis2f48d892011-05-09 08:05:43 +020025 *newname*.
Georg Brandl116aa622007-08-15 14:28:22 +000026
27
Georg Brandlcd7f32b2009-06-08 09:13:45 +000028.. class:: ModuleFinder(path=None, debug=0, excludes=[], replace_paths=[])
Georg Brandl116aa622007-08-15 14:28:22 +000029
30 This class provides :meth:`run_script` and :meth:`report` methods to determine
31 the set of modules imported by a script. *path* can be a list of directories to
32 search for modules; if not specified, ``sys.path`` is used. *debug* sets the
Neal Norwitz752abd02008-05-13 04:55:24 +000033 debugging level; higher values make the class print debugging messages about
Georg Brandl116aa622007-08-15 14:28:22 +000034 what it's doing. *excludes* is a list of module names to exclude from the
35 analysis. *replace_paths* is a list of ``(oldpath, newpath)`` tuples that will
36 be replaced in module paths.
37
38
Benjamin Petersone41251e2008-04-25 01:59:09 +000039 .. method:: report()
Georg Brandl116aa622007-08-15 14:28:22 +000040
Benjamin Petersone41251e2008-04-25 01:59:09 +000041 Print a report to standard output that lists the modules imported by the
42 script and their paths, as well as modules that are missing or seem to be
43 missing.
Georg Brandl116aa622007-08-15 14:28:22 +000044
Benjamin Petersone41251e2008-04-25 01:59:09 +000045 .. method:: run_script(pathname)
Georg Brandl116aa622007-08-15 14:28:22 +000046
Benjamin Petersone41251e2008-04-25 01:59:09 +000047 Analyze the contents of the *pathname* file, which must contain Python
48 code.
Georg Brandl116aa622007-08-15 14:28:22 +000049
Benjamin Petersone41251e2008-04-25 01:59:09 +000050 .. attribute:: modules
Georg Brandl116aa622007-08-15 14:28:22 +000051
Benjamin Petersone41251e2008-04-25 01:59:09 +000052 A dictionary mapping module names to modules. See
53 :ref:`modulefinder-example`
Christian Heimesd3eb5a152008-02-24 00:38:49 +000054
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
Neal Norwitz752abd02008-05-13 04:55:24 +000083 print('Loaded modules:')
84 for name, mod in finder.modules.items():
85 print('%s: ' % name, end='')
Ezio Melotti2befd9a2009-09-13 08:08:32 +000086 print(','.join(list(mod.globalnames.keys())[:3]))
Christian Heimesd3eb5a152008-02-24 00:38:49 +000087
Neal Norwitz752abd02008-05-13 04:55:24 +000088 print('-'*50)
89 print('Modules not imported:')
90 print('\n'.join(finder.badmodules.keys()))
Christian Heimesd3eb5a152008-02-24 00:38:49 +000091
92Sample output (may vary depending on the architecture)::
93
94 Loaded modules:
95 _types:
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +000096 copyreg: _inverted_registry,_slotnames,__all__
Christian Heimesd3eb5a152008-02-24 00:38:49 +000097 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