dependency-analyzer について
dependency-analyzerは、Pythonファイルやプロジェクトのインポートと依存関係を調べるスキルです。モジュールの分類、未使用・循環インポートの検出、requirements.txtの生成に対応します。
主な機能
- 01Pythonインポートの抽出
- 02標準・外部・ローカルモジュールの分類
- 03インポート関係の可視化
- 04未使用・循環インポートの検出
- 05requirements.txtの生成
活用シーン
- 01Pythonファイルのインポート確認
- 02プロジェクトの依存関係の把握
- 03未使用インポートの整理
- 04requirements.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 の生成: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
CLI の使用方法
# 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": {...}
}
}
ワークフローの例
requirements の生成
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 対応