mirror of
https://github.com/All-Hands-AI/OpenHands.git
synced 2026-01-10 07:18:10 -05:00
Fix and simplify local runtime init (#7997)
This commit is contained in:
@@ -5,6 +5,7 @@ This runtime runs the action_execution_server directly on the local machine with
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
from typing import Callable
|
||||
@@ -204,38 +205,21 @@ class LocalRuntime(ActionExecutionClient):
|
||||
env = os.environ.copy()
|
||||
# Get the code repo path
|
||||
code_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_path
|
||||
env['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)
|
||||
|
||||
check_dependencies(code_repo_path, poetry_venvs_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 using the derived env_root_path
|
||||
check_dependencies(code_repo_path, env_root_path)
|
||||
self.server_process = subprocess.Popen( # noqa: ASYNC101
|
||||
cmd,
|
||||
stdout=subprocess.PIPE,
|
||||
|
||||
Reference in New Issue
Block a user