importlib_resources
importlib_resources copied to clipboard
Defer `tempfile` import to sites of use
Part of #326.
tempfile is a relatively heavy import that's only used in a few routines, and they aren't always called when using importlib_resources, especially in the common case of as_file() being passed a pathlib.Path object. Thus, I think it's worthwhile to defer importing it until it's actually used.
This PR effectively inlines the one function using tempfile at module-level scope to avoid an import-time dependency on tempfile. It then "inlines" the necessary import in the two functions that use it.
Based on local testing, doing so improves the upfront import time of importlib_resourcesby about ~30% on supported CPython versions and ~50% on PyPy3.10.