title: Best practice for lazy loading Python modules
body: <p>Occasionally I want lazy module loading in Python. Usually because I want to keep runtime requirements or start-up times low and splitting the code into sub-modules would be cumbersome. A typical use case and my currently preferred implementation is this:</p>
  
  <pre><code>jinja2 = None
  
  class Handler(...):
      ...
      def render_with_jinja2(self, values, template_name):
          global jinja2
          if not jinja2:
              import jinja2
          env = jinja2.Environment(...)
          ...
  </code></pre>
  
  <p>I wonder: is there a canonical/better way to implement lazy module loading?</p>