A Python dependency tree analyzer with rich terminal output
Project description
PyDepTree
A powerful Python dependency analyzer that visualizes module dependencies in your Python projects as a beautiful tree structure. Built with Rich for colorful terminal output.
๐ธ Alternative Demo (Click to expand)
โจ Enhanced CLI Demo: The screenshot above shows PyDepTree's enhanced features including color-coded file types, file statistics, lint detection, and summary tables.
Features
Core Features
- ๐ฏ Smart Import Detection: Uses AST parsing to accurately find all imports
- ๐ณ Beautiful Tree Visualization: Rich-powered colorful dependency trees
- ๐ Configurable Depth: Control how deep to analyze dependencies
- ๐ Fast & Efficient: Skips standard library and external packages
- ๐จ Import Preview: See actual import statements with
--show-code - ๐ Progress Tracking: Real-time progress for large codebases
- ๐ Circular Dependency Detection: Identifies and handles circular imports
Enhanced Features โจ
- ๐จ Color-coded File Types: Models (๐), Services (๐), Utils (๐ง), Tests (๐งช), Main (๐)
- ๐ File Statistics: Size, line count, and import count badges for each file
- ๐ Lint Integration: Automatic error/warning detection using ruff (when available)
- ๐ Summary Tables: Aggregate statistics by file type with quality metrics
- ๐ฏ Enhanced Visualization: Rich terminal output with progress indicators and legends
Installation
Using pip
pip install pydeptree
Using pipx (recommended)
pipx install pydeptree
From source
git clone https://github.com/tfaucheux/pydeptree.git
cd pydeptree
pip install -e .
Enhanced Features Installation
For lint checking capabilities, install with enhanced dependencies:
pip install -e ".[enhanced]"
This installs ruff for code quality analysis.
Usage
Basic Usage
Analyze a Python file and see its direct dependencies:
pydeptree myapp.py
Enhanced Usage
Use the enhanced CLI for additional features:
pydeptree-enhanced myapp.py --depth 2
The enhanced version provides color-coded file types, lint checking, and detailed statistics.
Advanced Options
# Analyze dependencies up to 3 levels deep
pydeptree myapp.py --depth 3
# Show import statements from each file
pydeptree myapp.py --show-code
# Specify a custom project root
pydeptree myapp.py --project-root /path/to/project
Enhanced CLI Example Output
python -m pydeptree.cli_enhanced sample_project/main.py --depth 2
โญโโโโโโโโโ Analysis Settings โโโโโโโโโโฎ
โ Enhanced Python Dependency Analyzer โ
โ โ
โ File: sample_project/main.py โ
โ Project root: sample_project โ
โ Max depth: 2 โ
โ Lint checking: enabled โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
Legend:
๐ Models | ๐ Services | ๐ง Utils | ๐งช Tests | ๐ Main | Size | Lines |
Importsโ | E:Errors | W:Warnings
โญโโโโโโโโโโโโโโโโโโ Dependency Tree โโโโโโโโโโโโโโโโโโโฎ
โ ๐ main.py 741B 31L 2โ W:5 โ
โ โโโ ๐ services/api.py 2.1KB 71L 5โ W:13 โ
โ โ โโโ ๐ models/response.py 1.4KB 54L 3โ W:5 โ
โ โ โโโ ๐ง utils/http.py 1.8KB 55L 3โ E:2 W:14 โ
โ โโโ ๐ง utils/config.py 1.4KB 53L 5โ W:13 โ
โ โโโ ๐ models/settings.py 1.1KB 47L 2โ W:4 โ
โ โโโ ๐ง utils/validators.py 1.0KB 39L 2โ E:1 W:3 โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
File Statistics Summary
โโโโโโโโโโโโโโณโโโโโโโโณโโโโโโโโโโโโโโณโโโโโโโโโโโโณโโโโโโโโโณโโโโโโโโโโโ
โ Type โ Count โ Total Lines โ Avg Lines โ Errors โ Warnings โ
โกโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฉ
โ ๐ main โ 1 โ 31 โ 31 โ - โ 5 โ
โ ๐ model โ 2 โ 101 โ 50 โ - โ 9 โ
โ ๐ service โ 1 โ 71 โ 71 โ - โ 13 โ
โ ๐ง utils โ 3 โ 147 โ 49 โ 3 โ 30 โ
โโโโโโโโโโโโโโผโโโโโโโโผโโโโโโโโโโโโโโผโโโโโโโโโโโโผโโโโโโโโโผโโโโโโโโโโโค
โ Total โ 7 โ 350 โ 50 โ 3 โ 57 โ
โโโโโโโโโโโโโโดโโโโโโโโดโโโโโโโโโโโโโโดโโโโโโโโโโโโดโโโโโโโโโดโโโโโโโโโโโ
Quick Start Examples
Basic Usage
# Analyze a Python file with the original CLI
pydeptree myapp.py
# Use the enhanced version with additional features
pydeptree-enhanced myapp.py --depth 2
Enhanced Features Examples
# Disable lint checking
pydeptree-enhanced myapp.py --no-check-lint
# Disable statistics table
pydeptree-enhanced myapp.py --no-show-stats
# Show detailed import statements
pydeptree-enhanced myapp.py --show-code --depth 3
Testing the Enhanced Features
# Run the interactive demo
python demo_enhanced.py
# Try on the sample project (contains intentional lint errors for demo)
pydeptree-enhanced sample_project/main.py --depth 2
# Compare with original CLI
pydeptree sample_project/main.py --depth 2
Demo and Sample Project
PyDepTree includes a comprehensive sample project to demonstrate its enhanced features.
โ ๏ธ Note about Sample Project: The sample_project/ directory contains intentional code quality issues (linting errors, warnings, and code smells) to demonstrate the enhanced PyDepTree's lint checking capabilities. These are not bugs but deliberate examples that showcase how the tool can help identify code quality problems in real projects.
The sample project includes realistic examples of:
- Missing imports and type hints
- Unused variables
- Long lines exceeding style guidelines
- Inefficient code patterns
- Complex conditions that could be simplified
This allows you to see how PyDepTree Enhanced detects and reports these issues with color-coded badges and summary statistics.
Command Line Options
Basic CLI (pydeptree)
FILE_PATH: Path to the Python file to analyze (required)-d, --depth INTEGER: Maximum depth to traverse (default: 1)-r, --project-root PATH: Project root directory (default: file's parent)-c, --show-code: Display import statements from each file--help: Show help message and exit
Enhanced CLI (pydeptree-enhanced)
All basic options plus:
-l, --check-lint / --no-check-lint: Enable/disable lint checking (default: enabled)-s, --show-stats / --no-show-stats: Show/hide statistics summary table (default: enabled)
How It Works
PyDepTree uses Python's built-in AST (Abstract Syntax Tree) module to parse Python files and extract import statements. It then:
- Identifies which imports are part of your project (vs external libraries)
- Recursively analyzes imported modules up to the specified depth
- Builds a dependency graph while detecting circular imports
- Renders a beautiful tree visualization using Rich
Development
Setup Development Environment
# Clone the repository
git clone https://github.com/tfaucheux/pydeptree.git
cd pydeptree
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install in development mode with dev dependencies
pip install -e ".[dev]"
Running Tests
# Run all tests (33 tests total: 5 basic + 28 enhanced)
pytest
# Run with coverage report (should show ~85% coverage)
pytest --cov=pydeptree --cov-report=term-missing
# Run only enhanced CLI tests
pytest tests/test_cli_enhanced.py
# Run linting (Note: sample_project/ contains intentional errors for demo purposes)
ruff check pydeptree/ # Check only the main package code (clean)
ruff check . # Check everything (will show demo errors)
black --check .
mypy pydeptree
Note: The sample_project/ directory contains intentional linting errors for demonstration purposes. When running linting tools on the entire project, you'll see these demo errors alongside any real issues in the main codebase.
Building for Distribution
# Install build tools
pip install build twine
# Build distribution packages
python -m build
# Upload to TestPyPI (for testing)
twine upload --repository testpypi dist/*
# Upload to PyPI (for release)
twine upload dist/*
Notes
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pydeptree-0.2.1.tar.gz.
File metadata
- Download URL: pydeptree-0.2.1.tar.gz
- Upload date:
- Size: 17.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f3ecc61fa69bfb4ffddf1a26f67f5bdea6c15eac3d04e36ea72bacb9442ace49
|
|
| MD5 |
4edbee86aa96fd0d03a338523dd4fb3a
|
|
| BLAKE2b-256 |
d87cba74d59912e6dad44b3e931aee067d0c50095b65294ed36b56bedbf56c18
|
File details
Details for the file pydeptree-0.2.1-py3-none-any.whl.
File metadata
- Download URL: pydeptree-0.2.1-py3-none-any.whl
- Upload date:
- Size: 14.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c4ad1c027c7fe4a98b91a1b6aeb15cbf02ad6c506b0bcf84885061df47957488
|
|
| MD5 |
cbf1c2ccc75b53293b4ede2e7d6bb688
|
|
| BLAKE2b-256 |
7b6e6268781971c6395853d9a77478c2b4e5b085dcabcdaa75a9d54c3ad7d57e
|