summaryrefslogtreecommitdiff
path: root/devdocs/python~3.12/library%2Fmodulefinder.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/python~3.12/library%2Fmodulefinder.html
new repository
Diffstat (limited to 'devdocs/python~3.12/library%2Fmodulefinder.html')
-rw-r--r--devdocs/python~3.12/library%2Fmodulefinder.html65
1 files changed, 65 insertions, 0 deletions
diff --git a/devdocs/python~3.12/library%2Fmodulefinder.html b/devdocs/python~3.12/library%2Fmodulefinder.html
new file mode 100644
index 00000000..2bd0f78c
--- /dev/null
+++ b/devdocs/python~3.12/library%2Fmodulefinder.html
@@ -0,0 +1,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">
+ &copy; 2001&ndash;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>