Skip to content

import lyopronto pulls in matplotlib even when never plotting #37

Description

@bernalde

lyopronto/high_level.py imports matplotlib at module scope:

import matplotlib.pyplot as plt
from matplotlib import rc as matplotlibrc

lyopronto/__init__.py imports high_level, so every import lyopronto initializes matplotlib and its backend — including for callers that only run simulations and never plot. On this machine:

>>> import lyopronto, sys
>>> any(m.startswith("matplotlib") for m in sys.modules)
True

matplotlib is only used by generate_visualizations and the four _plot_* helpers. Nothing else in the package touches it.

This costs import time for every consumer, and it makes matplotlib a hard import-time dependency for headless use (batch simulation, CI, a server-side API) where a missing or misconfigured backend then fails at import rather than at the point of use.

Suggested fix — load it in generate_visualizations, which is the only public entry point that plots, and pass plt to the private helpers rather than relying on a module global. After that change, import lyopronto reports matplotlib loaded: False.

Happy to open a PR; I have the change prepared against 4fb63df.

Found while working on SECQUOIA/LyoPRONTO, which carries this change.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions