From 9876f2e9708b09ff590034560f55817eac1c4c59 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 | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/runhouse/resources/module.py b/runhouse/resources/module.py index 581f657a8..b6d550109 100644 --- a/runhouse/resources/module.py +++ b/runhouse/resources/module.py @@ -4,6 +4,7 @@ import inspect import logging import os +import site import sys from concurrent.futures import ThreadPoolExecutor from pathlib import Path @@ -368,8 +369,13 @@ 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 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()