π OpenHands: AI-Driven Development
Fix and simplify local runtime init (#7997)
Engel Nystb3bd3924a090
diff --git a/openhands/runtime/impl/local/local_runtime.py b/openhands/runtime/impl/local/local_runtime.pyindex a469baced8..f647b68fdd 100644--- a/openhands/runtime/impl/local/local_runtime.py+++ b/openhands/runtime/impl/local/local_runtime.py@@ -5,6 +5,7 @@ This runtime runs the action_execution_server directly on the local machine withimport osimport shutilimport subprocess+import sysimport tempfileimport threadingfrom typing import Callable@@ -204,38 +205,21 @@ class LocalRuntime(ActionExecutionClient):env = os.environ.copy()# Get the code repo pathcode_repo_path = os.path.dirname(os.path.dirname(openhands.__file__))- env['PYTHONPATH'] = f'{code_repo_path}:$PYTHONPATH'+ env['PYTHONPATH'] = f'{code_repo_path}{os.pathsep}{env.get("PYTHONPATH", "")}'env['OPENHANDS_REPO_PATH'] = code_repo_pathenv['LOCAL_RUNTIME_MODE'] = '1'- # Get the poetry venv path using 'poetry env info --path'- try:- poetry_venvs_path = subprocess.check_output( # noqa: ASYNC101- ['poetry', 'env', 'info', '--path'],- env=env,- cwd=code_repo_path,- text=True,- stderr=subprocess.PIPE,- shell=False,- ).strip()- # Verify it's a valid path (basic check)- if not os.path.isdir(poetry_venvs_path):- raise ValueError(f"'{poetry_venvs_path}' is not a valid directory.")- except (subprocess.CalledProcessError, FileNotFoundError, ValueError) as e:- # Attempt to fall back to environment variable if set- poetry_venvs_path = env.get('POETRY_VIRTUALENVS_PATH', '')- if not poetry_venvs_path or not os.path.isdir(poetry_venvs_path):- raise RuntimeError(- 'Cannot find poetry venv path using `poetry env info --path` or POETRY_VIRTUALENVS_PATH env var. '- 'Please check your poetry installation and ensure a virtual environment exists.'- ) from e- logger.warning(- f'Using fallback POETRY_VIRTUALENVS_PATH: {poetry_venvs_path}'- )- env['POETRY_VIRTUALENVS_PATH'] = poetry_venvs_path- logger.debug(f'POETRY_VIRTUALENVS_PATH: {poetry_venvs_path}')+ # Derive environment paths using sys.executable+ interpreter_path = sys.executable+ python_bin_path = os.path.dirname(interpreter_path)+ env_root_path = os.path.dirname(python_bin_path)++ # Prepend the interpreter's bin directory to PATH for subprocesses+ env['PATH'] = f'{python_bin_path}{os.pathsep}{env.get("PATH", "")}'+ logger.debug(f'Updated PATH for subprocesses: {env["PATH"]}')- check_dependencies(code_repo_path, poetry_venvs_path)+ # Check dependencies using the derived env_root_path+ check_dependencies(code_repo_path, env_root_path)self.server_process = subprocess.Popen( # noqa: ASYNC101cmd,stdout=subprocess.PIPE,diff --git a/openhands/runtime/plugins/jupyter/__init__.py b/openhands/runtime/plugins/jupyter/__init__.pyindex 05f4bbeb05..34ac5691bf 100644--- a/openhands/runtime/plugins/jupyter/__init__.py+++ b/openhands/runtime/plugins/jupyter/__init__.py@@ -48,13 +48,7 @@ class JupyterPlugin(Plugin):'OPENHANDS_REPO_PATH environment variable is not set. ''This is required for the jupyter plugin to work with LocalRuntime.')- # assert POETRY_VIRTUALENVS_PATH is set- poetry_venvs_path = os.environ.get('POETRY_VIRTUALENVS_PATH')- if not poetry_venvs_path:- raise ValueError(- 'POETRY_VIRTUALENVS_PATH environment variable is not set. '- 'This is required for the jupyter plugin to work with LocalRuntime.'- )+ # The correct environment is ensured by the PATH in LocalRuntime.poetry_prefix = f'cd {code_repo_path}\n'jupyter_launch_command = (f"{prefix}/bin/bash << 'EOF'\n"