Skip to content

Conversation

@tlambert03
Copy link
Collaborator

While working on #86, I noticed that scyjava was greedily importing pandas, even when it wasn't being used. Pandas is somewhat large and can slow down startup; so it would be nice to delay that until we know we need it.

The traditional pattern of try import just to know whether something is importable can be improved:

  1. if you want to know whether something is importable, you can use importlib.util.find_spec
  2. if you want to know whether someone has already imported something (e.g. to check whether some user object is an instance of some third-party type, without unnecessary import), you can look in sys.modules: sys.modules.get('pandas')

this PR removes the _import_X(required=False) pattern in place of importlib.util.find_spec

@tlambert03 tlambert03 marked this pull request as ready for review August 23, 2025 13:10
Copy link
Member

@ctrueden ctrueden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Glad to know there is a way to register these converters more efficiently.

),
]
if _import_pandas(required=False):
if find_spec("pandas"):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great trick, thanks!

@ctrueden ctrueden merged commit 477ea98 into scijava:main Aug 23, 2025
7 checks passed
@tlambert03 tlambert03 deleted the delay-import branch August 23, 2025 19:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants