内容表

  • modulefinder — 查找用于脚本的模块
    • Example usage of ModuleFinder

上一话题

pkgutil — 包扩展实用程序

下一话题
就业培训     下载中心     Wiki     联络
登录   注册

Log
  1. 首页
  2. Python 3.12.4
  3. 索引
  4. 模块
  5. 下一
  6. 上一
  7. Python 标准库
  8. 导入模块
  9. modulefinder — 查找用于脚本的模块

modulefinder — 查找用于脚本的模块 ¶

源代码: Lib/modulefinder.py


This module provides a ModuleFinder class that can be used to determine the set of modules imported by a script. modulefinder.py 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.

modulefinder. AddPackagePath ( pkg_name , path ) ¶

Record that the package named pkg_name can be found in the specified path .

modulefinder. ReplacePackage ( oldname , newname ) ¶

Allows specifying that the module named oldname is in fact the package named newname .

class modulefinder. ModuleFinder ( path = None , debug = 0 , excludes = [] , replace_paths = [] ) ¶

This class provides run_script() and report() methods to determine the set of modules imported by a script. path can be a list of directories to search for modules; if not specified, sys.path 被使用。 debug sets the debugging level; higher values make the class print debugging messages about what it’s doing. excludes is a list of module names to exclude from the analysis. replace_paths 是列表化的 (oldpath, newpath) tuples that will be replaced in module paths.

report ( ) ¶

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.

run_script ( pathname ) ¶

Analyze the contents of the pathname file, which must contain Python code.

模块 ¶

A dictionary mapping module names to modules. See Example usage of ModuleFinder .

Example usage of ModuleFinder ¶

The script that is going to get analyzed later on (bacon.py):

import re, itertools
try:
    import baconhameggs
except ImportError:
    pass
try:
    import guido.python.ham
except ImportError:
    pass
										

The script that will output the report of bacon.py:

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()))
										

Sample output (may vary depending on the architecture):

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
										

内容表

  • modulefinder — 查找用于脚本的模块
    • Example usage of ModuleFinder

上一话题

pkgutil — 包扩展实用程序

下一话题

runpy — 定位和执行 Python 模块

本页

  • 报告 Bug
  • 展示源

快速搜索

键入搜索术语或模块、类、函数名称。

  1. 首页
  2. Python 3.12.4
  3. 索引
  4. 模块
  5. 下一
  6. 上一
  7. Python 标准库
  8. 导入模块
  9. modulefinder — 查找用于脚本的模块

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1