/python-tooling:uv-worktree-venv-fallthroughfrom python-tooling
Fix for phantom Python test failures in fresh git worktrees of uv-managed projects, caused by `uv run` falling through to a PATH-resolved command (e.g. homebrew Python 3.9's pytest) when the worktree's `.venv` doesn't have the command installed. Use when: (1) `uv run pytest --collect-only -q` in a freshly-created worktree reports `ModuleNotFoundError: No module named 'msgspec'` (or any other project runtime dep) on test files that import the package; (2) you see `ImportError: cannot import name 'UTC' from 'datetime'` or `dataclass() got an unexpected keyword argument 'slots'` despite `pyproject.toml` requiring Python 3.10+/3.12+; (3) the same `uv run pytest` succeeds on the main worktree but fails on a sibling worktree; (4) `which pytest` (or `which ruff`, `which mypy`) inside the worktree resolves to `/opt/homebrew/bin/...` instead of `<worktree>/.venv/bin/...`; (5) you're orchestrating parallel agents in worktree-isolation mode and they report wildly inconsistent test results vs the main tree. Root cause is `uv run`'s fallthrough to `$PATH` when the venv doesn't have the command, combined with empty `.venv` in fresh worktrees. Fix is one command: `uv sync --extra dev`.
Trending skills
7-day visitors to each skill page
Ships in python-tooling. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
uv run <cmd> in a uv-managed Python project will run <cmd> from
the project's .venv/bin/ if it exists there. If it doesn't, uv run falls through to $PATH. Combined with git worktree add
(which gives the new worktree an empty .venv), this silently picks
up the system Python's pytest / ruff / mypy — typically
homebrew's Python 3.9 on macOS — instead of the project's pinned
3.12+. The wrong-Python interpreter then can't import 3.10+/3.12+
language features, producing errors that LOOK like code bugs but
are actually environment bugs.
This is especially painful in parallel-agent workflows: each agent's worktree starts with an empty venv, and every agent re-discovers the same trap before any test runs.
Any of these:
uv run pytest --collect-only -q in a fresh worktree raises
ModuleNotFoundError on a runtime dep that's clearly listed in
pyproject.toml:
ImportError while importing test module 'tests/test_schemas.py'.
ModuleNotFoundError: No module named 'msgspec'
The error mentions a Python feature your pyproject.toml requires
by minimum version, but the runtime is older:
ImportError: cannot import name 'UTC' from 'datetime'
TypeError: dataclass() got an unexpected keyword argument 'slots'
which pytest from inside the worktree resolves to
/opt/homebrew/bin/pytest or /usr/bin/pytest, not
<worktree>/.venv/bin/pytest.
The exact same uv run pytest invocation from the project's main
worktree (or ~/.../<repo> checkout) succeeds.
A subagent dispatched to a worktree reports test-collection failures
that the main session can't reproduce on main.
uv run resolves commands in this order:
<cwd>/.venv/bin/<cmd>$PATHWhen you run git worktree add (or any tool that creates an isolated
worktree — including the isolation: "worktree" option on the Agent
tool), the new worktree:
pyproject.toml and uv.lock..venv directory..venv (or none) until you run uv sync for the
first time.So uv run pytest sees no .venv/bin/pytest and falls through to
homebrew's pytest, which is bound to homebrew's Python 3.9. That
Python tries to import the project's package, hits 3.10+ language
features (datetime.UTC, dataclass(slots=True), match statements,
PEP 604 X | Y unions, etc.), and crashes.
The error messages point at the project's source files, so the natural reaction is "the project's code is broken" — but the project code is fine; it's just being parsed by the wrong interpreter.
One command, run from inside the worktree, before any uv run:
cd <worktree>
uv sync --extra dev
This populates <worktree>/.venv/ with the project's runtime + dev
dependencies, including a venv-local pytest binary that's bound to
the correct Python (the one matching pyproject.toml's
requires-python).
After uv sync, the next uv run pytest resolves to
<worktree>/.venv/bin/pytest and the trap is gone.
# 1. .venv now has pytest
ls .venv/bin/pytest && head -1 .venv/bin/pytest
# Expect first line is a shebang pointing into the worktree's .venv:
# #!<worktree>/.venv/bin/python
# 2. uv run resolves correctly
which pytest # may still show homebrew (PATH unchanged)
uv run which pytest # MUST show <worktree>/.venv/bin/pytest
# 3. Collection is green
uv run pytest --collect-only -q | tail -3
# Expect: "<N> tests collected" with no ModuleNotFoundError
Every agent dispatched with isolation: "worktree" (or any flow
that calls git worktree add before running Python commands) MUST
run uv sync --extra dev as its first command, before any other
uv run invocation. Otherwise the agent will spend cycles
investigating phantom failures that don't exist on main.
Prompt template addition (paste into agent dispatch prompts that target uv-managed Python repos):
First action in the worktree: `uv sync --extra dev`. Do not skip
this — fresh worktrees have an empty .venv and `uv run` will fall
through to system Python (often the wrong version), producing
phantom ModuleNotFoundError / ImportError that look like code bugs.
A repo-level convenience: a Makefile target or
scripts/init-worktree.sh that wraps uv sync --extra dev so the
agent only has to run one named command.
A subagent reports:
"pre-existing pytest collection errors on main"
- test_schemas.py: ModuleNotFoundError: No module named 'msgspec'
- test_orchestrator.py: ImportError: cannot import name 'UTC' from 'datetime'
- test_team_loader.py: TypeError: dataclass() got an unexpected keyword argument 'slots'
Diagnostic steps:
# Reproduce in main worktree?
cd ~/Projects/workspace/<repo>
uv run pytest --collect-only -q | tail -3
# "174 tests collected" — clean. So it's worktree-specific.
# In the failing worktree:
cd <worktree>
which pytest # /opt/homebrew/bin/pytest ← system Python!
uv run which pytest # /opt/homebrew/bin/pytest ← uv falling through!
ls .venv/bin/pytest 2>&1 # ls: .venv/bin/pytest: No such file or directory
# The fix:
uv sync --extra dev # ~10s in this repo
uv run which pytest # <worktree>/.venv/bin/pytest ← correct
uv run pytest --collect-only -q | tail -3
# "174 tests collected" — green.
Total resolution time: under a minute once the pattern is
recognized. Without this skill, expect 20–40 minutes chasing the
"UTC not in datetime" error through the codebase before realizing
it's an environment bug.
pip install failures are loud, and poetry run
raises if the env isn't set up. uv's fallthrough is a feature
(intentional convenience for ad-hoc invocations) that becomes a
trap inside worktrees.uv run ruff,
uv run mypy, uv run vulture, uv run python -m <module>, etc.
All of them fall through to PATH if the venv lacks the binary.uv.lock drift between worktree and main —
the lock file is shared (it's tracked in git), only the
realized .venv is per-worktree.[project.optional-dependencies] dev = [...], use uv sync (no --extra) — same effect for
runtime deps, just without dev tools. But you usually want dev
tools in a worktree because you're about to run them..venv from main into the worktree
to "share" envs, expect uv to refuse and rebuild. Don't try to
sidestep uv sync with a symlink — just run it.run semantics:
https://docs.astral.sh/uv/reference/cli/#uv-runsync semantics:
https://docs.astral.sh/uv/reference/cli/#uv-synccajias/nautilus-competition worktree (May 2026). The
worktree had no .venv/bin/pytest, so uv run pytest fell through
to homebrew's Python 3.9, which couldn't import msgspec (not
installed in 3.9's site-packages) and crashed on
dataclass(slots=True) (3.9 lacks the kwarg). After uv sync --extra dev, all 174 tests collected green and the "errors" were
retroactively classified as a NO-OP environmental issue.What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub cajias/claude-skills --plugin python-tooling5 skills in this plugin
/importlib-reload-class-identityDiagnose and fix the silent class-identity drift caused by `importlib.reload()`
on modules that define classes whose instances live elsewhere. Use when:
(1) a pytest fixture uses `importlib.reload(some_module)` to "flush caches"
and downstream tests then fail with `isinstance` checks rejecting objects
that LOOK correct; (2) you see a diagnostic warning of the form "expected X,
got X" — same class name, different class identity (`type(obj).__name__ ==
"X"` but `obj is not isinstance(X)`); (3) one fragile test fixture causes
dozens-to-hundreds of cascading failures in unrelated test files; (4) tests
pass in isolation but fail when run as part of the full suite, with order
dependence pointing at the reload site; (5) you have an entry-point /
plugin / discovery system whose post-reload registry is empty even though
the underlying entries still exist. Covers root cause (Python class objects
are first-class identities; reload mints new ones; pre-reload instances
keep old identity), the diagnostic signature, and the canonical fix
(subprocess isolation via `subprocess.run([sys.executable, "-c", ...])`
or `pytest-forked` — never `importlib.reload` for class-exposing modules).
/kronos-pypi-collisionFix for `pip install kronos` returning the wrong package when trying to use
the financial K-line foundation model. Use when: (1) you intend to use the
Kronos OHLCV/K-line transformer from arXiv 2508.02739 (Shi et al., AAAI 2026),
(2) `pip install kronos` succeeds but `import kronos` fails with
`ModuleNotFoundError: No module named 'django'` (the unrelated PyPI package
is a Django-based scheduler), (3) you can't find a `kronos` pip package that
exposes `Kronos`/`KronosTokenizer`/`KronosPredictor`. The financial Kronos
model has no published pip package under that name; install from the GitHub
repo (`shiyu-coder/Kronos`) instead. Also covers Mac (Apple Silicon) usage:
device must be `"cpu"` or `"mps"`, never `"cuda:0"`.
/python-slots-mock-patch-objectFix for `AttributeError: '<ClassName>' object attribute '<method>' is
read-only` raised during `mock.patch.object()` teardown. Use when:
(1) a Python test using `unittest.mock.patch.object(instance, "method")`
passes the patched code but raises an AttributeError during context-exit
(visible in the stack as `mock.py:1635` calling `delattr(self.target,
self.attribute)`); (2) the target class declares `__slots__`; (3) the
test was working before someone added `__slots__` to a base class or
the target class itself; (4) a refactor introduced `__slots__` "for
efficiency" and broke a previously-green test suite. Covers root cause
(`__slots__` blocks `delattr` because it doesn't allocate `__dict__`,
so the attribute can't be removed after the patch sets it), the
diagnostic signature, the canonical fix (drop `__slots__` if the class
is a mock target), and when `__slots__` IS the right call (when memory
matters AND the class is never patched).
/ruff-jupyter-diff-inflationPrevent or diagnose massive PR diffs (700-1000+ lines per .ipynb) when
running `ruff check --fix` or `make lint-fix` in a Python project that
checks in Jupyter notebooks. Use when: (1) a lint cleanup PR explodes
from a small expected diff to thousands of lines; (2) `git diff` of a
notebook shows `source` field changing from a single string to an array
of lines (or vice versa); (3) deciding how to scope ruff in a project
with both .py files and committed notebooks. Covers the structural
Jupyter source-format normalization that ruff performs and the
pyproject.toml change to suppress it.
Other plugins' skills for the same job