1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
<span id="modulefinder-find-modules-used-by-a-script"></span><h1>modulefinder — Find modules used by a script</h1> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/modulefinder.py">Lib/modulefinder.py</a></p> <p>This module provides a <a class="reference internal" href="#modulefinder.ModuleFinder" title="modulefinder.ModuleFinder"><code>ModuleFinder</code></a> class that can be used to determine the set of modules imported by a script. <code>modulefinder.py</code> can also be run as a script, giving the filename of a Python script as its argument, after which a report of the imported modules will be printed.</p> <dl class="py function"> <dt class="sig sig-object py" id="modulefinder.AddPackagePath">
<code>modulefinder.AddPackagePath(pkg_name, path)</code> </dt> <dd>
<p>Record that the package named <em>pkg_name</em> can be found in the specified <em>path</em>.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="modulefinder.ReplacePackage">
<code>modulefinder.ReplacePackage(oldname, newname)</code> </dt> <dd>
<p>Allows specifying that the module named <em>oldname</em> is in fact the package named <em>newname</em>.</p> </dd>
</dl> <dl class="py class"> <dt class="sig sig-object py" id="modulefinder.ModuleFinder">
<code>class modulefinder.ModuleFinder(path=None, debug=0, excludes=[], replace_paths=[])</code> </dt> <dd>
<p>This class provides <a class="reference internal" href="#modulefinder.ModuleFinder.run_script" title="modulefinder.ModuleFinder.run_script"><code>run_script()</code></a> and <a class="reference internal" href="#modulefinder.ModuleFinder.report" title="modulefinder.ModuleFinder.report"><code>report()</code></a> methods to determine the set of modules imported by a script. <em>path</em> can be a list of directories to search for modules; if not specified, <code>sys.path</code> is used. <em>debug</em> sets the debugging level; higher values make the class print debugging messages about what it’s doing. <em>excludes</em> is a list of module names to exclude from the analysis. <em>replace_paths</em> is a list of <code>(oldpath, newpath)</code> tuples that will be replaced in module paths.</p> <dl class="py method"> <dt class="sig sig-object py" id="modulefinder.ModuleFinder.report">
<code>report()</code> </dt> <dd>
<p>Print a report to standard output that lists the modules imported by the script and their paths, as well as modules that are missing or seem to be missing.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="modulefinder.ModuleFinder.run_script">
<code>run_script(pathname)</code> </dt> <dd>
<p>Analyze the contents of the <em>pathname</em> file, which must contain Python code.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="modulefinder.ModuleFinder.modules">
<code>modules</code> </dt> <dd>
<p>A dictionary mapping module names to modules. See <a class="reference internal" href="#modulefinder-example"><span class="std std-ref">Example usage of ModuleFinder</span></a>.</p> </dd>
</dl> </dd>
</dl> <section id="example-usage-of-modulefinder"> <span id="modulefinder-example"></span><h2>Example usage of ModuleFinder</h2> <p>The script that is going to get analyzed later on (bacon.py):</p> <pre data-language="python">import re, itertools
try:
import baconhameggs
except ImportError:
pass
try:
import guido.python.ham
except ImportError:
pass
</pre> <p>The script that will output the report of bacon.py:</p> <pre data-language="python">from modulefinder import ModuleFinder
finder = ModuleFinder()
finder.run_script('bacon.py')
print('Loaded modules:')
for name, mod in finder.modules.items():
print('%s: ' % name, end='')
print(','.join(list(mod.globalnames.keys())[:3]))
print('-'*50)
print('Modules not imported:')
print('\n'.join(finder.badmodules.keys()))
</pre> <p>Sample output (may vary depending on the architecture):</p> <pre data-language="python">Loaded modules:
_types:
copyreg: _inverted_registry,_slotnames,__all__
re._compiler: isstring,_sre,_optimize_unicode
_sre:
re._constants: REPEAT_ONE,makedict,AT_END_LINE
sys:
re: __module__,finditer,_expand
itertools:
__main__: re,itertools,baconhameggs
re._parser: _PATTERNENDERS,SRE_FLAG_UNICODE
array:
types: __module__,IntType,TypeType
---------------------------------------------------
Modules not imported:
guido.python.ham
baconhameggs
</pre> </section> <div class="_attribution">
<p class="_attribution-p">
© 2001–2023 Python Software Foundation<br>Licensed under the PSF License.<br>
<a href="https://docs.python.org/3.12/library/modulefinder.html" class="_attribution-link">https://docs.python.org/3.12/library/modulefinder.html</a>
</p>
</div>
|