GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — refactor: delay import of pandas until needed by tlambert03 · Pull Request #87 · scijava/scyjava

Conversation

@tlambert03

@tlambert03 tlambert03 commented Aug 23, 2025

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@tlambert03
refactor: delay import of pandas until needed
9e33f2e
@tlambert03
tlambert03 marked this pull request as ready for review August 23, 2025 13:10
@tlambert03
remove reveal type
6f89935
ctrueden
ctrueden approved these changes Aug 23, 2025
View reviewed changes

@ctrueden ctrueden left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this 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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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

@ctrueden ctrueden Aug 23, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

tlambert03 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@ctrueden
ctrueden merged commit 477ea98 into scijava:main Aug 23, 2025
@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

Reviewers

@ctrueden ctrueden ctrueden approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@tlambert03 @ctrueden
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.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.