On this page
modulefinder
Prev: mmap | Up: stdlib_module | Next: msvcrt
Rel: sys, sysconfig, builtins, warnings, dataclasses
Ref: docs.python.org — modulefinder
Tags: #public #python3 #stdlib
Import
import modulefinder
Overview
Static import-graph analyzer (ModuleFinder) that inspects bytecode to discover all modules a Python script imports, along with missing (badmodules) imports. Use modulefinder to audit script dependencies, build custom bundlers/packagers, or verify that a standalone script has no unexpected third-party imports.
Quick Start
import modulefinder
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
script = Path(tmp) / "app.py"
script.write_text("import json\nimport pathlib\n", encoding="utf-8")
finder = modulefinder.ModuleFinder()
finder.run_script(str(script))
assert "json" in finder.modules and "pathlib" in finder.modules
Implementation Walkthrough
1. Detecting Missing or Optional Dependencies
finder.badmodules maps unresolved module names to the modules that tried to import them:
import modulefinder
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
script = Path(tmp) / "check.py"
script.write_text("import nonexistent_pkg_xyz\n", encoding="utf-8")
finder = modulefinder.ModuleFinder()
finder.run_script(str(script))
assert "nonexistent_pkg_xyz" in finder.badmodules
Gotchas & Boundaries
- Cannot see dynamic imports: Calls to
importlib.import_module(var),__import__(...), or plugin entry points are invisible to static bytecode scanning. - No namespace packages / custom meta_path hooks:
modulefinderuses its own import emulation rather than fullimportlibmachinery, so PEP 420 namespace packages or zip/custom loaders may be missed. - Slow on large dependency graphs: Restrict
path=or passexcludes=[...]when analyzing scripts in large virtual environments.
Common Functions
- modulefinder.AddPackagePath() —
AddPackagePath(packagename, path)—modulefinder.AddPackagePath()function. - modulefinder.ReplacePackage() —
ReplacePackage(oldname, newname)—modulefinder.ReplacePackage()function. - modulefinder.test() —
test()—modulefinder.test()function.
Classes & Objects
- modulefinder.Module —
modulefinder.Moduleclass. - modulefinder.ModuleFinder —
modulefinder.ModuleFinderclass. (Methods:.add_module(),.any_missing(),.any_missing_maybe(),.determine_parent(),.ensure_fromlist(),.find_all_submodules())
Constants & Attributes
modulefinder.packagePathMap(dict) ={}modulefinder.replacePackageMap(dict) ={}
Linked from
Related notes
- tuts/python3/stdlib_module/mmap Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/msvcrt Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/builtins Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/dataclasses Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/modulefinder/AddPackagePath Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/modulefinder/Module Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/modulefinder/ModuleFinder Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/modulefinder/ReplacePackage Linked here; shared tags: #stdlib