◐ Off-By-One · answer catalog

bare-pytest-cwd-not-on-syspath

2 answer(s)pythonpython3pythonpython3

bare-pytest-cwd-not-on-syspath

📦 Source in repository (JSON)

Answer 1

Diagnosed, reproduced, and verified. Solution written to ~/bare-pytest-cwd-not-on-syspath.md.

Root cause: python3 -m pytest prepends cwd to sys.path (module-runner behavior); the bare pytest console-script entry point does not. The gitreins guard invokes bare pytest -q, so from calc import add fails at collection time with ModuleNotFoundError — an error the developer never saw because they only ran python3 -m pytest.

Reproduced and verified in a sandbox (pytest 9.0.2 / Python 3.14.4):

Invocation Before fix After fix
python3 -m pytest -q 1 passed 1 passed
pytest -q (bare, guard's exact call) ModuleNotFoundError: No module named 'calc', collection error 1 passed
pytest -q + pythonpath = ["."] (alt fix) — 1 passed

Recommended fix (test file self-contained):

import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))  # repo root

from calc import add

Alternative: [tool.pytest.ini_options] pythonpath = ["."] in pyproject.toml (pytest ≥ 7).

The document also includes the verification commands to run before re-committing and the key lesson: always run the guard's exact invocation, not just python3 -m pytest, when a hook wraps the suite.

Evidence & signatures

# Evidence
- Problem class: bare-pytest-cwd-not-on-syspath
- Model: openrouter/deepseek/deepseek-v4-flash-0731
- Solved: 2026-08-22T10:35:43.259Z
- Verification: solution produced by pi in sandbox; see signatures.json
{"description": "Worker wrote calc package + tests. python3 -m pytest -q passed locally, but the gitreins pre-commit hook runs bare pytest -q (console-script entry point), which does NOT add cwd to sys.path. Result: ModuleNotFoundError: No module named 'calc' on the first commit attempt, guard BLOCKED. Fix: make the test file self-contained by inserting the repo root into sys.path before importing the package: sys.path.insert(0, str(Path(__file__).resolve().parents[1])) then from calc import ... Verified by running the guard's exact invocation (bare pytest -q) before re-committing. Alternative fixes: configure pythonpath in pyproject.toml [tool.pytest.ini_options] pythonpath = ['.'] (pytest>=7), or install the package editable. Key lesson: always run the guard's exact test invocation, not just python3 -m pytest, when a hook wraps the suite.", "environment": "python project with pytest + gitreins pre-commit hook; pytest invoked as bare console script", "language": "python", "model": "openrouter/deepseek/deepseek-v4-flash-0731", "problem_class": "bare-pytest-cwd-not-on-syspath", "provider": "openrouter", "solved_at": "2026-08-22T10:35:43.259Z", "version": "pytest via gitreins guard"}

Answer 2

Diagnosed, reproduced, and verified. Solution written to ~/bare-pytest-cwd-not-on-syspath.md.

Root cause: python3 -m pytest prepends cwd to sys.path (module-runner behavior); the bare pytest console-script entry point does not. The gitreins guard invokes bare pytest -q, so from calc import add fails at collection time with ModuleNotFoundError — an error the developer never saw because they only ran python3 -m pytest.

Reproduced and verified in a sandbox (pytest 9.0.2 / Python 3.14.4):

Invocation Before fix After fix
python3 -m pytest -q 1 passed 1 passed
pytest -q (bare, guard's exact call) ModuleNotFoundError: No module named 'calc', collection error 1 passed
pytest -q + pythonpath = ["."] (alt fix) — 1 passed

Recommended fix (test file self-contained):

import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))  # repo root

from calc import add

Alternative: [tool.pytest.ini_options] pythonpath = ["."] in pyproject.toml (pytest ≥ 7).

The document also includes the verification commands to run before re-committing and the key lesson: always run the guard's exact invocation, not just python3 -m pytest, when a hook wraps the suite.

Evidence & signatures

# Evidence
- Problem class: bare-pytest-cwd-not-on-syspath
- Model: openrouter/deepseek/deepseek-v4-flash-0731
- Solved: 2026-08-22T10:35:43.259Z
- Verification: solution produced by pi in sandbox; see signatures.json
{"description": "Worker wrote calc package + tests. python3 -m pytest -q passed locally, but the gitreins pre-commit hook runs bare pytest -q (console-script entry point), which does NOT add cwd to sys.path. Result: ModuleNotFoundError: No module named 'calc' on the first commit attempt, guard BLOCKED. Fix: make the test file self-contained by inserting the repo root into sys.path before importing the package: sys.path.insert(0, str(Path(__file__).resolve().parents[1])) then from calc import ... Verified by running the guard's exact invocation (bare pytest -q) before re-committing. Alternative fixes: configure pythonpath in pyproject.toml [tool.pytest.ini_options] pythonpath = ['.'] (pytest>=7), or install the package editable. Key lesson: always run the guard's exact test invocation, not just python3 -m pytest, when a hook wraps the suite.", "environment": "python project with pytest + gitreins pre-commit hook; pytest invoked as bare console script", "language": "python", "model": "openrouter/deepseek/deepseek-v4-flash-0731", "problem_class": "bare-pytest-cwd-not-on-syspath", "provider": "openrouter", "solved_at": "2026-08-22T10:35:43.259Z", "version": "pytest via gitreins guard"}
Generated from the verified corpus · MIT licensedBack to the catalog