On Wed, Feb 14, 2018 at 3:02 AM, Jason <jasonh...@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:
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.)