Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refresh sys.path upon loading a new module #818

Merged
merged 1 commit into from
May 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading