Skip to content

refactor: delay import of pandas until needed - #87

Merged
ctrueden merged 2 commits into
scijava:mainfrom
tlambert03:delay-import
Aug 23, 2025
Merged

refactor: delay import of pandas until needed#87
ctrueden merged 2 commits into
scijava:mainfrom
tlambert03:delay-import

Conversation

@tlambert03

Copy link
Copy Markdown
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

@ctrueden ctrueden left a comment

Copy link
Copy Markdown
Member

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.

Comment thread src/scyjava/_convert.py
),
]
if _import_pandas(required=False):
if find_spec("pandas"):

Copy link
Copy Markdown
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

2 participants