perf: implement lazy imports for heavy dependencies#1321
Merged
dimitri-yatsenko merged 2 commits intopre/v2.0from Jan 9, 2026
Merged
perf: implement lazy imports for heavy dependencies#1321dimitri-yatsenko merged 2 commits intopre/v2.0from
dimitri-yatsenko merged 2 commits intopre/v2.0from
Conversation
Defer loading of heavy dependencies (networkx, matplotlib, click, pymysql) until their associated features are accessed: - dj.Diagram, dj.Di, dj.ERD -> loads diagram.py (networkx, matplotlib) - dj.kill -> loads admin.py (pymysql via connection) - dj.cli -> loads cli.py (click) This reduces `import datajoint` time significantly, especially on macOS where import overhead is higher. Core functionality (Schema, Table, Connection, etc.) remains immediately available. Closes #1220 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Cache lazy imports in globals() to override the submodule that importlib automatically sets on the parent module - Add dj.diagram to lazy modules (returns module for diagram_active access) - Add tests for cli callable and diagram module access Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implement lazy imports to reduce
import datajointtime, especially on macOS where import overhead is significantly higher (~6s vs ~1s on Linux).Problem
Heavy dependencies are loaded eagerly at import time even if unused:
diagram.pyimportsnetworkxandmatplotlib(~3s on Mac)admin.pyimportspymysqlvia connection (~2.5s on Mac)cli.pyimportsclickSolution
Use
__getattr__in__init__.pyto defer loading until first access:Results
Before (estimated from issue):
import datajointAfter:
What's lazy vs eager
Eager (always loaded):
Lazy (loaded on first access):
dj.Diagram,dj.Di,dj.ERD→ networkx, matplotlibdj.kill→ admin moduledj.cli→ clickTest plan
Closes #1220
🤖 Generated with Claude Code