From eba80d7d5115d9001707ae82cf780442fd468111 Mon Sep 17 00:00:00 2001 From: dongreenberg Date: Tue, 21 May 2024 17:32:52 -0400 Subject: [PATCH] Refresh sys.path upon loading a new module --- runhouse/resources/module.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/runhouse/resources/module.py b/runhouse/resources/module.py index 581f657a8..1dc184aa6 100644 --- a/runhouse/resources/module.py +++ b/runhouse/resources/module.py @@ -4,8 +4,10 @@ import inspect import logging import os +import site import sys from concurrent.futures import ThreadPoolExecutor +from importlib import reload as importlib_reload from pathlib import Path from typing import Callable, Dict, List, Optional, Tuple, Type, Union @@ -368,8 +370,15 @@ def _get_obj_from_pointers(module_path, module_name, obj_name, reload=True): """Helper method to load a class or function from a module path, module name, and class name.""" if module_path: abs_path = str((Path.home() / module_path).expanduser().resolve()) - sys.path.insert(0, abs_path) - logger.debug(f"Appending {module_path} to sys.path") + if not abs_path: + logger.debug(f"Could not find module path {module_path}") + elif abs_path not in sys.path: + sys.path.insert(0, abs_path) + logger.debug(f"Appending {module_path} to sys.path") + + # This updates the sys.path with any new paths that have been added since the last time we imported + # e.g. if the user ran cluster.run(["pip install my_package"]) since this env was created. + importlib_reload(site) if module_name in obj_store.imported_modules and reload: importlib.invalidate_caches()