code: def test_import_from_path() -> None:
      @contextlib.contextmanager
      def prepend_sys_path(path: str) -> Iterator:
          old = sys.path
          sys.path = [path] + sys.path
          try:
              yield
          finally:
              sys.path = old
  
      fixture = (
          pathlib.Path(__file__).parent
          / "fixtures"
          / "import_from_path"
      )
      # Modify sys.path so that lib1.py and lib2.py are treated as if it were an installed library.
      # Installed libraries should not be affected by the caching behavior
      with prepend_sys_path(str(fixture / "libraries")):
          # Import lib1 before the checkpoints do.
          import lib1
  
          # Execute from the a/ directory like we were in a normal interactive interpreter.
          with filetree.chdir(fixture / "a"), prepend_sys_path(""):
              import model_def as a  # noqa: I2001
  
              with det.import_from_path(fixture / "b"):
                  import model_def as b  # noqa: I2001
  
                  # Nesting is not supported.
                  with pytest.raises(
                      RuntimeError, match="does not support nesting"
                  ):
                      with det.import_from_path(fixture / "c"):
                          pass
  
              with det.import_from_path(fixture / "c"):
                  import model_def as c  # noqa: I2001
  
          # Import lib2 after the checkpoints do.
          import lib2
  
      # Each module should have its own val and data_val.
      assert a.val == "a", a.val
      assert a.data_val == "a", a.data_val
      assert b.val == "b", b.val
      assert b.data_val == "b", b.data_val
      assert c.val == "c", c.val
      assert c.data_val == "c", c.data_val
  
      # Each module must share the same lib1 and lib2, from outside import_from_path.
      assert id(lib1) == a.lib1_id == b.lib1_id == c.lib1_id
      assert id(lib2) == a.lib2_id == b.lib2_id == c.lib2_id
  
      # Ensure no cache files got created in the import_from_apaths
      assert not os.path.exists(fixture / "b" / "__pycache__")
      assert not os.path.exists(fixture / "c" / "__pycache__")
  
      # We will have created some __pycache__ dirs but we don't want to pollute.
      shutil.rmtree(fixture / "a" / "__pycache__")
      shutil.rmtree(fixture / "libraries" / "__pycache__")
  
      # These modules may leak into other imports in tests downstream
      for mod in ["lib1", "lib2", "data"]:
          sys.modules.pop(mod)
  
file_path: determined/harness/tests/test_imports.py
imports: import contextlib
  import os
  import pathlib
  import shutil
  import subprocess
  import sys
  import textwrap
  from typing import Iterator
  import pytest
  import determined
  from tests import filetree
label_desc: Use image node_id 2 for calling a global function with example usage: test_import_from_path() without return types