Skip to content

Conversation

@hugovk
Copy link
Member

@hugovk hugovk commented Jan 22, 2025

It can be useful to defer imports so that when people import tablib but don't use certain features (for example, if they don't use JSON format) they can avoid the import cost.

This particularly helps people writing tools like CLIs where startup cost can be a problem.

If people are using tablib and other libraries, the total import times can all add up.

We've been doing this also in the stdlib, for example:

Running:

python3 -X importtime -c 'import tablib' 2> import.log
pip install tuna
tuna import.log

Before: 8 ms

image

After: 2 ms

image

@claudep
Copy link
Contributor

claudep commented Jan 22, 2025

I think this makes sense when you are loading known slow external packages. But as far as the standard lib is concerned, I'm seeing this as some "overoptimization". So not very fond of this patch, sorry.

@hugovk
Copy link
Member Author

hugovk commented Jan 22, 2025

Small improvements multiplied by millions of users and computers could definitely help avoid some of the compute power concerns in #607, but okay!

@claudep
Copy link
Contributor

claudep commented Jan 22, 2025

I was waiting for such an answer 🤣 You make a point!

Until now, I learned from Python best practices that "embedded" imports were mostly reserved to cases where there was circular imports. If you have some articles about that "conflict" between best practices and optimizations (I guess it's some sort of balance to do), I'm interested.

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