Repository navigation
[python-package] make some imports lazy: 'dask', 'graphviz', 'joblib', 'matplotlib', 'psutil' - #7340
Merged
Merged
Conversation
jameslamb
marked this pull request as ready for review
June 23, 2026 20:41
jameslamb
requested review from
StrikerRUS,
borchero,
guolinke,
jmoralez,
mayer79 and
shiyu1994
as code owners
June 23, 2026 20:41
Member
Author
|
@jmoralez could you help review this one if you have time? I recall talking with you about |
jmoralez
approved these changes
Jun 25, 2026
jmoralez
left a comment
Collaborator
There was a problem hiding this comment.
Thanks! Totally support this
Co-authored-by: José Morales <jmoralz92@gmail.com>
Member
Author
|
Great, thanks for the quick review! I'm gonna try to apply this treatment to the Let's see if the impact of what I come up with is worth the complexity. I'll push what I come up with so you can at least see it, either way. |
This was referenced Jul 12, 2026
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.
Contributes to #7327
For years,
lightgbmhas handled optional dependencies and compatibility patches incompat.py. This has served the project well by standardizing the way those things are handled and reducing repetition.But thinking hard about import times, I realized a significant drawback of this approach... centralizing all those things means that all those optional dependencies are eagerly loaded by the first
lightgbmimport. In other words, ifdaskormatplotlibis installed in the environment,import lightgbmwill cause it to be imported, even if your code doesn't need it.That memory usage and time might not be meaningful for training workloads, but it definitely could be for inference-only workloads.
This is the first in a series of PRs aimed at addressing that. It proposes making the following imports lazy:
daskgraphvizjoblibmatplotlibpsutilNotes for Reviewers
Impact + How I tested this
Created a script that times a cold (first-time) and cached (not first time)
import lightgbm.import-timings.sh (click me)
Compared to the baseline from #7327, as of this PR the first
import lightgbmon my M2 Mac is about 4 seconds faster.I don't think much of that is attributable to dropping
pyarrowimports in #7275, as I still seepyarrowin the output ofpython -X importtime.I calculated where
import lightgbmis spending its time like this:output (click me)
Compare that to the baseline from #7327 and you'll see that some of the most expensive imports like
matplotlibandPILhave dropped out entirely. The remaining time is dominated bylightgbmitself,scikit-learn, and `scipy.A note about macOS
macOS has a system called "GateKeeper" that scans every
.dylibthe first time it's loaded: https://support.apple.com/en-us/102445Python libraries primarily pull in shared libraries (
.dylib) through the following:lib_lightgbm.dylib)libgomp.dylib)So these import timings above from my Mac probably don't give a good indication about the timings you could expect on Linux or Windows... but they DO give a rough idea of the impact of these changes, and an exact enumeration of which modules are imported when you
import lightgbm.This also partially explains why the first "cold" import takes so much longer than subsequent imports.
Why label this
breaking?I tried to make this fully backwards-compatible and not break any user code. But it could change the import order in user code, and we've seen issues that import-order dependent here before (e.g. #6509 and #6595).
Proposing calling it
breakingto make this change more visible in release notes.