Skip to content

feat(experiment): add python_path to make modules importable in task workers - #292

Merged
bpiwowar merged 2 commits into
experimaestro:masterfrom
model-collapse:feat/experiment-python-path
Oct 21, 2026
Merged

bpiwowar merged 2 commits into
experimaestro:masterfrom
model-collapse:feat/experiment-python-path

Conversation

@model-collapse

Copy link
Copy Markdown
Contributor

Task workers reconstruct the task module by file path and do not inherit the launching interpreter's sys.path. A Task.execute() that imports a module by name at run time (plugin registries, factories, sibling packages) therefore fails with ModuleNotFoundError when driving experiments programmatically via experiment(), which exposed no way to add worker import paths (project_paths is only used for git info).

Add an optional python_path argument to experiment(); its entries are resolved and appended to workspace.python_path, which the script builder already injects into the worker's sys.path. Equivalent to exporting PYTHONPATH before submitting, but discoverable from the API.

Adds a regression test (plus a strict-xfail control proving the fix is load-bearing).

…workers

Task workers reconstruct the task module by file path and do not inherit the
launching interpreter's sys.path. A Task.execute() that imports a module by name
at run time (plugin registries, factories, sibling packages) therefore fails
with ModuleNotFoundError when driving experiments programmatically via
experiment(), which exposed no way to add worker import paths (project_paths is
only used for git info).

Add an optional `python_path` argument to experiment(); its entries are resolved
and appended to workspace.python_path, which the script builder already injects
into the worker's sys.path. Equivalent to exporting PYTHONPATH before submitting,
but discoverable from the API.

Adds a regression test (plus a strict-xfail control proving the fix is
load-bearing).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@VictorMorand

Copy link
Copy Markdown
Contributor

hi @model-collapse ! Thanks a lot for your contribution, we were mostly launching experiments through the CLI and never had this issue :)

@bpiwowar

Copy link
Copy Markdown
Collaborator

Thanks @model-collapse, this is a useful addition. The CLI already fills workspace.python_path from the pythonpath YAML key, but nothing exposed it when you drive experiments through experiment() directly. The approach looks right to me: it reuses the path the script builder already injects into workers, so nothing else needs to change.

A few things before merging:

  1. Type hints: the project targets Python 3.10+ and uses the modern syntax. Could you write python_path: list[Path | str] | None = None instead of Optional[list[Union[Path, str]]]?
  2. Formatting: the test file doesn't look ruff-formatted (for example, the line wrapping of the experiment(...) calls). Please install the hooks and run them (pre-commit install && pre-commit run --all-files); see CONTRIBUTING.md.
  3. Documentation: the docstring already appears in the API reference. Could you also add a short note to docs/source/experiments.md, under "Two ways to run", right after the with experiment(...) example? It should say that tasks importing modules by name at run time need python_path=[...], and that this is the counterpart of the pythonpath YAML key of run-experiment (see experiments/cli-runner.md).

Minor points, not blocking:

  • Each entry is inserted with sys.path.insert(0, …), so with several paths the last one is searched first. Worth a word in the docstring, or reverse the order when extending, if you want the list order to be the search order.
  • The strict-xfail control relies on the worker really not seeing the temporary folder. That holds today, but an unusual PYTHONPATH or working directory in some environment could make it fail. It might be safer to clear PYTHONPATH in that test with monkeypatch.delenv("PYTHONPATH", raising=False).
  • The commit author is shown as "ClusterBench noreply@anthropic.com". Could you re-author it under your own name (git commit --amend --reset-author)? Otherwise we'll squash-merge so you get the credit.

Thanks again!

- modern type hints for python_path
- entries are searched in the order given (sys.path.insert reverses them)
- document python_path in experiments.md
- clear PYTHONPATH in the strict-xfail control test
- ruff-format the test

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bpiwowar
bpiwowar merged commit 49db410 into experimaestro:master Oct 21, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants