关于 dependency-analyzer
dependency-analyzer 是用于检查 Python 文件和项目导入及依赖的技能,可分类模块、检测未使用导入和循环依赖,并生成 requirements.txt。
主要功能
- 01提取 Python 导入
- 02分类标准库、第三方和本地模块
- 03可视化导入关系
- 04检测未使用及循环导入
- 05生成 requirements.txt
适用场景
- 01检查 Python 文件的导入
- 02梳理项目依赖关系
- 03清理未使用的导入
- 04生成 requirements.txt
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ dkyazzentwatwa/ chatgpt-skills --skill dependency-analyzer安装后在 Claude Code 中输入/dependency-analyzer确认已加载。
附带文件scripts/dependency_analyzer.pyscripts/requirements.txt
详细介绍
依赖分析器
分析 Python 文件的导入项和项目依赖。
功能
- 导入项提取:列出 Python 文件中的所有导入项
- 依赖关系图:可视化导入关系
- 未使用项检测:查找未使用的导入项
- 依赖项生成:自动生成 requirements.txt
- 标准库检测:区分标准库与第三方库
- 循环导入检测:查找循环依赖
快速开始
from dependency_analyzer import DependencyAnalyzer
analyzer = DependencyAnalyzer()
# Analyze single file
imports = analyzer.analyze_file("main.py")
print(imports)
# Analyze project
result = analyzer.analyze_project("./src")
print(result['third_party']) # External dependencies
命令行用法
# Analyze single file
python dependency_analyzer.py --file main.py
# Analyze project directory
python dependency_analyzer.py --dir ./src
# Generate requirements.txt
python dependency_analyzer.py --dir ./src --requirements --output requirements.txt
# Find unused imports
python dependency_analyzer.py --file main.py --unused
# Show dependency graph
python dependency_analyzer.py --dir ./src --graph
# JSON output
python dependency_analyzer.py --dir ./src --json
API 参考
DependencyAnalyzer 类
class DependencyAnalyzer:
def __init__(self)
# Analysis
def analyze_file(self, filepath: str) -> dict
def analyze_project(self, directory: str) -> dict
# Detection
def find_unused_imports(self, filepath: str) -> list
def find_circular_imports(self, directory: str) -> list
# Generation
def generate_requirements(self, directory: str) -> list
def save_requirements(self, deps: list, output: str)
# Classification
def is_stdlib(self, module: str) -> bool
def is_local(self, module: str, directory: str) -> bool
输出格式
文件分析
{
"file": "main.py",
"imports": [
{"module": "os", "type": "stdlib", "line": 1},
{"module": "json", "type": "stdlib", "line": 2},
{"module": "requests", "type": "third_party", "line": 3},
{"module": "utils.helpers", "type": "local", "line": 4}
],
"from_imports": [
{"module": "typing", "names": ["Dict", "List"], "type": "stdlib"},
{"module": "flask", "names": ["Flask", "request"], "type": "third_party"}
]
}
项目分析
{
"directory": "./src",
"files_analyzed": 15,
"stdlib": ["os", "sys", "json", "typing", ...],
"third_party": ["requests", "flask", "pandas", ...],
"local": ["utils", "models", "config", ...],
"by_file": {
"main.py": {...},
"app.py": {...}
}
}
示例工作流
生成依赖项
analyzer = DependencyAnalyzer()
deps = analyzer.generate_requirements("./src")
analyzer.save_requirements(deps, "requirements.txt")
查找未使用的导入项
analyzer = DependencyAnalyzer()
unused = analyzer.find_unused_imports("main.py")
for imp in unused:
print(f"Line {imp['line']}: {imp['module']} is unused")
分析项目结构
analyzer = DependencyAnalyzer()
result = analyzer.analyze_project("./myproject")
print("Third-party dependencies:")
for dep in result['third_party']:
print(f" - {dep}")
print("\nLocal modules:")
for mod in result['local']:
print(f" - {mod}")
检查循环导入
analyzer = DependencyAnalyzer()
circular = analyzer.find_circular_imports("./src")
if circular:
print("Circular imports detected:")
for cycle in circular:
print(f" {' -> '.join(cycle)}")
模块分类
| 类型 | 描述 | 示例 |
|---|---|---|
stdlib | Python 标准库 | os、sys、json |
third_party | 外部软件包 | requests、pandas |
local | 项目模块 | utils.helpers |
依赖项
无外部依赖项——使用 Python 标准库(ast 模块)。
评论
支持 Markdown