FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

refactor: delay import of pandas until needed by tlambert03 · Pull Request #87 · scijava/scyjava · GitHub

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

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 marked this pull request as ready for review August 23, 2025 13:10

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This is a great trick, thanks!

ctrueden merged commit 477ea98 into scijava:main Aug 23, 2025
7 checks passed
tlambert03 deleted the delay-import branch August 23, 2025 19:38
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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL