Defer, ensure library is loaded
Chris Angelico
rosuav at gmail.com
Tue Feb 13 11:08:07 EST 2018
On Wed, Feb 14, 2018 at 3:02 AM, Jason <jasonhihn at gmail.com> wrote:
> I have a variety of scripts that import some large libraries, and rather than create a million little scripts with specific imports, I'd like to so something like
>
> psycopg2 = ensure_imported (psycopg2)
>
> This way, regardless of invocation I can know psycopg2 is loaded, if it hasn't already been loaded. If I just do a normal import 95% of the time I'll be waiting time with a meaningless import.
>
>
> Can I do that somehow?
Yep! It's written like this:
import psycopg2
If it's already been imported, Python will go fetch it straight from
the cache, so it's fast.
(The cache is in "sys.modules", if you're curious. "import sys" to
have a look at it.)
ChrisA
More information about the Python-list
mailing list