code: def template_to_file(
      template_file: str, dest: str, output_encoding: str, **kw: Any
  ) -> None:
      template = Template(filename=template_file)
      try:
          output = template.render_unicode(**kw).encode(output_encoding)
      except:
          with tempfile.NamedTemporaryFile(
              suffix=".txt", delete=False
          ) as ntf:
              ntf.write(
                  exceptions.text_error_template()
                  .render_unicode()
                  .encode(output_encoding)
              )
              fname = ntf.name
          raise CommandError(
              "Template rendering failed; see %s for a "
              "template-oriented traceback." % fname
          )
      else:
          with open(dest, "wb") as f:
              f.write(output)
  
imports: from __future__ import annotations
  import atexit
  from contextlib import ExitStack
  import importlib
  import importlib.machinery
  import importlib.util
  import os
  import re
  import tempfile
  from types import ModuleType
  from typing import Any
  from typing import Optional
  from mako import exceptions
  from mako.template import Template
  from .None import compat
  from .exc import CommandError
label_desc: Use image node_id 1 for calling a global function with example usage: template_to_file(template_file, dest, output_encoding) without return types