Skip to content

Commit

Permalink
Refresh sys.path upon loading a new module
Browse files Browse the repository at this point in the history
  • Loading branch information
dongreenberg committed May 22, 2024
1 parent 3e39eca commit eba80d7
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions runhouse/resources/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit eba80d7

Please sign in to comment.