Skip to content

fix(browser): strip PYTHONPATH/PYTHONHOME from browser-use CLI subprocess env - #83471

Closed
n1majne3 wants to merge 3 commits into
NousResearch:mainfrom
n1majne3:fix/strip-pythonpath-browser-subprocess
Closed

fix(browser): strip PYTHONPATH/PYTHONHOME from browser-use CLI subprocess env#83471
n1majne3 wants to merge 3 commits into
NousResearch:mainfrom
n1majne3:fix/strip-pythonpath-browser-subprocess

Conversation

@n1majne3

Copy link
Copy Markdown
Contributor

Problem

browser_exec crashes on every call when the agent process has PYTHONPATH pointing at the Hermes venv (e.g. the desktop app):

ModuleNotFoundError: No module named 'pydantic_core._pydantic_core'

The browser-use CLI runs under its own Python interpreter (via uvx or a standalone binary) — typically 3.12/3.13. The inherited PYTHONPATH points at Hermes's venv site-packages (Python 3.11), so the CLI imports compiled extensions like pydantic_core._pydantic_core.cpython-311-darwin.so from the wrong interpreter. The ABI mismatch causes the ModuleNotFoundError.

Root Cause

_base_subprocess_env() in tools/browser_use_cli.py delegates to _build_browser_env()hermes_subprocess_env(), which copies os.environ including PYTHONPATH. While hermes_subprocess_env() already strips VIRTUAL_ENV / CONDA_PREFIX via _ACTIVE_VENV_MARKER_VARS, PYTHONPATH is not stripped — it is the same class of cross-version leak.

Fix

Strip PYTHONPATH and PYTHONHOME from the browser-use subprocess env in _base_subprocess_env(). The CLI manages its own interpreter/venv and never needs Hermes's import path.

Test Plan

  • With PYTHONPATH set to Hermes venv site-packages, call browser_exec — should no longer crash.
  • Verify the browser-use CLI still launches and operates normally without PYTHONPATH.

Closes #83427

…cess env

The browser-use CLI runs under its own Python interpreter (via uvx or a
standalone binary), which may be a different version than Hermes's venv
Python (3.11). When the desktop app sets PYTHONPATH to point at the
Hermes venv site-packages, the CLI imports compiled extensions (e.g.
pydantic_core._pydantic_core.cpython-311-*.so) built for the wrong
interpreter version, causing:

    ModuleNotFoundError: No module named 'pydantic_core._pydantic_core'

hermes_subprocess_env() already strips VIRTUAL_ENV / CONDA_PREFIX via
_ACTIVE_VENV_MARKER_VARS for the same reason — PYTHONPATH is the same
class of leak and must be stripped from the browser-use subprocess env.

Closes NousResearch#83427
@ZaidNAlAsali

Copy link
Copy Markdown

Confirmed this fix against current upstream/main (ed5e17f4b) on Windows 10. In a real Hermes Desktop environment, inherited PYTHONPATH/PYTHONHOME caused the Browser Use CLI to import Hermes's Python 3.11 packages and fail at pydantic_core; after stripping both variables, a real browser_exec smoke test connected successfully to the running Brave CDP session.

I also tested a focused regression test for this PR and proved that it bites:

  • Pre-fix _base_subprocess_env(): fails, because PYTHONPATH remains in the child environment.
  • With this PR's fix: 1 passed.
  • Ruff on both affected files: passed.
  • Full tests/tools/test_browser_use_cli.py on Windows:
    • current upstream baseline: 57 passed, 10 failed
    • fix + new test: 58 passed, 10 failed
    • the same 10 failures occur on unmodified upstream and come from existing POSIX fake-CLI/path fixtures on Windows.

The current PR has the correct production change. This small test would make the regression contract explicit under TestBrowserExec:

def test_subprocess_env_strips_parent_python_import_paths(self, monkeypatch):
    """uvx must resolve its own isolated dependencies, not Hermes's venv."""
    monkeypatch.setattr(
        "tools.browser_tool._build_browser_env",
        lambda: {
            "PYTHONPATH": "C:/hermes;C:/hermes/venv/Lib/site-packages",
            "PYTHONHOME": "C:/hermes/venv",
            "KEEP_ME": "yes",
        },
    )

    env = bu_cli._base_subprocess_env()

    assert "PYTHONPATH" not in env
    assert "PYTHONHOME" not in env
    assert env["KEEP_ME"] == "yes"

This checks both isolation variables while confirming unrelated Browser Use environment values are preserved.

@teknium1

Copy link
Copy Markdown
Contributor

Merged on main via #86371 with your commit authorship preserved — thanks @n1majne3. Your branch carried unrelated desktop commits via a merge from main, so the browser hunk was surgically reapplied onto current main; the regression test suggested in this PR's review thread was included (both vars stripped, unrelated env preserved). You were the earliest of two independent fixes for this bug — credit to you as first submitter. Fixes #83427/#84841/#86006/#86104.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 Medium — degraded but workaround exists tool/browser Browser automation (CDP, Playwright) type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

browser_exec crashes: pydantic_core ModuleNotFoundError when PYTHONPATH points at Hermes venv (desktop app)

4 participants