Worker isolation¶
Each version runs in its own pytest-xdist worker, started with that version's
interpreter from .pytest-uv/py<version>. Everything the worker imports (your project,
its dependencies, pytest itself) must come from that environment. Otherwise a test that
claims to run on 3.11 might import a package built for 3.13.
What xdist does by default¶
When the controller starts a popen worker, xdist sends it the controller's
sys.path, and the worker replaces its own sys.path with it
(WorkerController.setup,
remote.py).
For workers that run the same interpreter as the controller this is harmless. For a
3.11 worker started from a 3.13 .venv, it means imports resolve to the 3.13
site-packages:
E ModuleNotFoundError: No module named 'hypothesis._native'
Here Hypothesis was found in the controller's 3.13 environment, whose compiled extension the 3.11 interpreter cannot load. Pure-Python packages may import without an error, which is worse: the test passes against the wrong copy of a dependency.
What pytest-uv does¶
When it prepares an environment, pytest-uv records that interpreter's own sys.path
(run with python -I, so without the current directory or PYTHONPATH) in the
environment's stamp file. As xdist sets up each worker, pytest-uv replaces the list it
is about to send with that worker's own sys.path. Two kinds of entries from the
controller are kept in front: the directory pytest was started from (the script
directory, or the current directory for python -m pytest) and the entries of
PYTHONPATH. So:
- third-party packages resolve under the worker's
sys.prefix; - your project is imported from the editable install in that environment;
- pytest, pluggy and xdist on the worker are the versions installed for that Python.
Check it¶
A throwaway test that records where things come from on each worker:
import sys
import pytest
def test_imports_come_from_the_worker_environment(python_version):
if python_version is None:
pytest.skip("run with --python-versions")
assert sys.version.startswith(python_version)
assert pytest.__file__.startswith(sys.prefix)
uv run pytest --python-versions 3.11,3.13 test_isolation.py -v