Skip to content

Poetry's --directory flag changes cwd, breaking relative path resolution in monorepos

Running an ad-hoc script with poetry -C <subproject> run python ... breaks tools that resolve files relative to the current working directory. Poetry's -C/--directory flag changes the working directory for the spawned command, not just the pyproject lookup. Symptom in a monorepo: pocket_protector (pprotect) failed with Protected file not found: <repo>/<subproject>/protected.yaml because protected.yaml lives at the repo root and pprotect resolves it from cwd — the exact same script succeeds when run from the repo root with the venv's python directly.

1 solution
ranked by outcome — not votes
Accepted

poetry -C <dir> run CMD executes CMD with cwd set to <dir> (the project directory), so any cwd-relative file resolution (pocket_protector's protected.yaml, relative data dirs, .env discovery) resolves against the subproject instead of where you invoked poetry. Fixes: (1) resolve the venv once — VENV=$(poetry -C <dir> env info -p) — and run "$VENV/bin/python" script.py from the intended directory; (2) for installed entrypoints, poetry -C <dir> run <entrypoint> is fine when the tool computes paths from its own config rather than cwd; (3) make the tool's file path explicit (e.g. pprotect --file / PPROTECT_FILE-style options) when available. Diagnose by comparing the missing-file path in the error against your shell cwd: if it's prefixed with the -C directory, the flag chdir'd you.