Import Performance Tools Usage GuideΒΆ
π― GoalΒΆ
Prevent future issues with slow imports and circular dependencies.
π¦ Installed ToolsΒΆ
1. Circular Import DetectionΒΆ
# Detect all circular imports
make check-circular
# Or run directly
python scripts/detect_circular_imports.py
Output example:
2. Heavy Import DetectionΒΆ
# Check all Python files
make check-heavy
# Or check specific files
python scripts/check_heavy_imports.py src/apflow/cli/commands/*.py
Will warn about these heavyweight libraries: - litellm (2.4s) - crewai (5.4s) - torch, transformers, tensorflow, langchain
3. Import Performance AnalysisΒΆ
# Full performance report
make check-performance
# Or use Python's built-in tool directly
python -X importtime -c "import apflow.cli.main" 2>&1 | grep apflow
4. One-Click Check AllΒΆ
π§ Pre-commit HooksΒΆ
Automatically configured checks that run before every commit:
# Install (first time only)
pip install pre-commit
pre-commit install
# Now every git commit will automatically check:
# β Circular imports
# β Heavyweight module-level imports
# β Code formatting (ruff)
π CI/CD Automatic ChecksΒΆ
Every PR will automatically verify: - β Circular import detection - β CLI startup time < 1.5 seconds - β No heavyweight dependencies loaded at CLI startup
See .github/workflows/import-performance.yml
π Development GuidelinesΒΆ
β Avoid These MistakesΒΆ
-
Module-level heavyweight imports
-
Package
__init__.pyEager Imports -
Automatically importing all Extensions
β Correct PracticesΒΆ
-
Import on Demand
-
Lazy Loading for Packages
-
Registry Lazy Loading
π Recommended Professional ToolsΒΆ
tuna - Visualize Import TimeΒΆ
pip install tuna
python -X importtime -c "import apflow" 2> import.log
tuna import.log # Opens browser with visual chart
pydeps - Dependency GraphΒΆ
import-profilerΒΆ
π Performance BenchmarksΒΆ
Current metrics: - β
CLI startup: 1.3 seconds (target < 1.5 seconds) - β
import apflow.core: 0.8 seconds - β
import apflow.cli.main: 0.7 seconds - β
Zero circular imports
Historical comparison: - Before optimization: 7.0 seconds π± - After optimization: 1.3 seconds β¨ - Improvement: 5.4Γ π
π οΈ Daily UsageΒΆ
Before Developing a New FeatureΒΆ
After Finishing DevelopmentΒΆ
# Run all checks
make check-imports
# Or individual checks
make check-circular
make check-performance
make check-heavy
Git CommitΒΆ
When Encountering WarningsΒΆ
Read docs/development/import-performance.md for detailed fix solutions.
π Learning ResourcesΒΆ
- docs/development/import-performance.md - Complete best practices guide
- PEP 562 - Module getattr
- Python Import System
π‘ RememberΒΆ
"If it takes more than 1 second to run
--help, it's too slow."
Import performance directly affects user experience. Stay vigilant and avoid: 1. Module-level heavyweight imports 2. Eager loading of all modules 3. Circular dependencies
Use these tools to continuously monitor! π―