Skip to main content

Analyze dependencies in a Python project

Project description

deptrace

Analyze dependencies in a Python project.

This project performs four main tasks:

  1. Analyze the (nested) scopes in a Python file.
  2. Analyze certain variable assignments in a Python file.
  3. Crawl the dependency graph of a Python file.
  4. Build call trees showing which functions call a target function.

Usage

Basic usage:

python -m deptrace <file_path>

Examples:

# Analyze dependencies (default)
python -m deptrace ./src/deptrace/processors/process_file.py

# Analyze call tree for a function across entire project
python -m deptrace src --action call-tree --target-function parse_file

Options

  • file_path: Path to the Python file or directory to analyze
  • --action: Type of analysis to perform: dependencies (default) or call-tree
  • --target-function: Target function name for call tree analysis (required with --action call-tree)
  • --depth: Depth of the analysis (default: 4)
  • --log-level: Set logging level (DEBUG, INFO) (default: INFO)
  • --scope-filter: Filter output to a specific scope (e.g., '.outer.Inner.method')
  • --output-file: Write results to specified file
  • --output-format: Format for output file (JSON) (default: JSON)

Examples with options:

# Dependency analysis with options
python -m deptrace ./src/deptrace/processors/process_file.py \
  --depth 6 \
  --log-level DEBUG \
  --scope-filter "<module>.my_function"

# Call tree analysis with output file
python -m deptrace src \
  --action call-tree \
  --target-function parse_file \
  --output-file ./call_tree.json \
  --log-level INFO

Example: Analyzing the parse_file Function

The parse_file function is one of the most heavily used functions in the deptrace codebase itself. It's responsible for parsing Python files into AST (Abstract Syntax Tree) format and is used by multiple subsystems. Let's analyze its call tree to understand how different parts of the project depend on it:

# Analyze how parse_file is used throughout the deptrace project
python -m deptrace src --action call-tree --target-function parse_file

This reveals that parse_file has:

  • 3 direct callers: process_file, build_graph, and analyze_project_call_tree
  • Multiple indirect callers through several call chains:
    • Processing pipeline: run_analysisanalyze_fileprocess_fileparse_file
    • Import crawler: crawlbuild_graphparse_file
    • Recursive imports: process_importsresolve_importbuild_graphparse_file

This demonstrates how call tree analysis helps you understand:

  • Which components depend on a critical function
  • The different code paths that lead to a function being called
  • The impact of changing a function's signature or behavior

Output File

When using --output-file, the analysis results will be written to the specified file in JSON format. For dependency analysis, this includes:

  • Scope analysis
  • Assignment information
  • Dependency graph information
  • Import relationships

For call tree analysis, this includes:

  • Target function name
  • Direct callers with call sites and arguments
  • Recursive caller relationships

Example with output file:

python -m deptrace ./src/deptrace/processors/process_file.py \
  --log-level DEBUG \
  --output-file ./deptrace.json

Programmatic API

Call Tree Analysis

Build call trees to understand which functions call a target function, both directly and indirectly:

from deptrace import analyze_call_tree, analyze_project_call_tree

# Analyze a single file
source_code = '''
def calculate_score(value, multiplier=1.0):
    return value * multiplier * 100

def process_data(data):
    score = calculate_score(len(data), 1.5)
    return score
'''

result = analyze_call_tree(source_code, "calculate_score")
print(result["direct_callers"])  # Shows process_data calls calculate_score

# Analyze an entire project
result = analyze_project_call_tree("/path/to/project", "target_function")

The call tree analysis handles:

  • Cross-file function calls
  • Import aliases (from utils import func as renamed_func)
  • Module.function calls (utils.func())
  • Recursive caller relationships (who calls the callers)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

deptrace-0.1.0.tar.gz (24.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

deptrace-0.1.0-py3-none-any.whl (39.1 kB view details)

Uploaded Python 3

File details

Details for the file deptrace-0.1.0.tar.gz.

File metadata

  • Download URL: deptrace-0.1.0.tar.gz
  • Upload date:
  • Size: 24.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.11

File hashes

Hashes for deptrace-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c74ac8d9eb6e4f34f955a16eb728574ea29ed7b384b1a083b5511aa082010ed7
MD5 edc77b3ce9cc71ea9cb16065c63d2372
BLAKE2b-256 f7f9ebe457e2b9ddc7b563dad97f5ac74f0924d80b5be6e3c7111571fd6f789e

See more details on using hashes here.

File details

Details for the file deptrace-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: deptrace-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 39.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.11

File hashes

Hashes for deptrace-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 056ea1c6688c6ca5f231f81014604796db733e5ed0dbdfb5c3c4aa712c76251b
MD5 0769c987314038ca7679e6b3fa7c3dea
BLAKE2b-256 7bb4e9b32d3632ab7914cc86d66c37e31e216327dca7b6210e8a17a2017c4b99

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page