From 4d6462247eedee6a57053dd791f8292593e6f54f Mon Sep 17 00:00:00 2001 From: Todd Gamblin Date: Sat, 28 Dec 2019 14:43:23 -0800 Subject: externals: avoid importing jinja2 on startup (#14308) Jinja2 costs a tenth to a few tenths of a second to import, so we should avoid importing it on startup. - [x] only import jinja2 within functions --- lib/spack/spack/modules/common.py | 3 ++- lib/spack/spack/tengine.py | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/spack/spack/modules/common.py b/lib/spack/spack/modules/common.py index 2e0090a449..5cacea561a 100644 --- a/lib/spack/spack/modules/common.py +++ b/lib/spack/spack/modules/common.py @@ -804,10 +804,11 @@ class BaseModuleFileWriter(object): # Get the template for the module template_name = self._get_template() + import jinja2 try: env = tengine.make_environment() template = env.get_template(template_name) - except tengine.TemplateNotFound: + except jinja2.TemplateNotFound: # If the template was not found raise an exception with a little # more information msg = 'template \'{0}\' was not found for \'{1}\'' diff --git a/lib/spack/spack/tengine.py b/lib/spack/spack/tengine.py index 6aceb391cd..f872ea49ad 100644 --- a/lib/spack/spack/tengine.py +++ b/lib/spack/spack/tengine.py @@ -5,7 +5,6 @@ import itertools import textwrap -import jinja2 import llnl.util.lang import six @@ -13,9 +12,6 @@ import spack.config from spack.util.path import canonicalize_path -TemplateNotFound = jinja2.TemplateNotFound - - class ContextMeta(type): """Meta class for Context. It helps reducing the boilerplate in client code. @@ -77,6 +73,10 @@ def make_environment(dirs=None): dirs = [canonicalize_path(d) for d in itertools.chain(builtins, extensions)] + # avoid importing this at the top level as it's used infrequently and + # slows down startup a bit. + import jinja2 + # Loader for the templates loader = jinja2.FileSystemLoader(dirs) # Environment of the template engine -- cgit v1.2.3-60-g2f50