code: def _local_execution_manager(context_dir: pathlib.Path) -> Iterator:
      """
      A context manager used for local execution to mimic the environment of trial
      container execution.
  
      It does the following things:
      1. Set the current working directory to be the context directory.
      2. Add the current working directory to importing paths.
      3. Catch SystemExit.
      """
      current_directory = os.getcwd()
      current_path = sys.path[0]
  
      try:
          os.chdir(str(context_dir))
  
          # Python typically initializes sys.path[0] as the empty string which directs
          # Python to search modules in the current directory first when invoked
          # interactively. We set sys.path[0] manually to let Python importer search the
          # the current directory first.
          # Reference: https://docs.python.org/3/library/sys.html#sys.path
          sys.path[0] = ""
          with det._catch_sys_exit():
              yield
      finally:
          os.chdir(current_directory)
          sys.path[0] = current_path
  
imports: import contextlib
  import logging
  import math
  import os
  import pathlib
  import random
  import sys
  import uuid
  from typing import Any, Dict, Iterator, List, Optional, Tuple, Type
  import determined
  from determined import constants, core, gpu, load
label_desc: Use image node_id 7 for calling a global function with example usage: _local_execution_manager(context_dir) without return types