astral-sh / uv

An extremely fast Python package and project manager, written in Rust.
https://docs.astral.sh/uv
Apache License 2.0
20.29k stars 603 forks source link

importlib.metadata.distributions() doesn't surface all installed packages/endpoints, breaking pytest plugins #6376

Closed ashbywinch closed 3 weeks ago

ashbywinch commented 4 weeks ago

I tried to migrate a project to uv, but pytest could no longer find its plugins pytest-cov and pytest-testmon.

$ uvx pytest --testmon
ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: unrecognized arguments: --testmon

On investigation it seems that pytest uses Pluggy to load the plugins, and Pluggy uses importlib.metadata.distributions() to identify and load installed packages that have entrypoints matching its naming convention (see Pluggy source)

I made an empty project with "uv init", added dependencies [ "pytest", "pytest-cov", "pytest-testmon" ], ran "uv sync" successfully (I can see the installed files in .venv/lib/site-packages) and ran the following code

for dist in list(importlib.metadata.distributions()):
    print("name: ", dist.name)
    for ep in dist.entry_points:
         print("ep: ", ep.name, ep.group)

Under my regular project this code prints (among other things):

name:  pytest-cov
ep:  pytest_cov pytest11
name:  pytest-testmon
ep:  pytest-testmon pytest11

Under my uv test project those modules/endpoints are not in the list, even though they have been successfully installed.

$ uv --version
uv 0.3.0 (dd1934c9c 2024-08-20)

and this is on Win11.

charliermarsh commented 4 weeks ago

I think the problem you're running into here is that uvx runs in an environment isolated from your project. Can you try uv run pytest instead? Or even uv run --with pytest pytest if it's not part of your project environment?

zanieb commented 4 weeks ago

Some documentation on this at https://docs.astral.sh/uv/concepts/tools/#relationship-to-uv-run

We may be able to improve the documentation though, if you have any questions.

ashbywinch commented 3 weeks ago

Yes, that's fixed it! Thanks 👍 I think it might have helped if this page here: https://docs.astral.sh/uv/guides/tools/ had a section right at the top clarifying why one would or wouldn't want to run a tool without installing, and pointing to the section on how to run a tool WITH installing (or without installing but with plugins, which is mentioned, but much further down the page). The info I needed was all there, but I didn't realise it would be relevant when I first read it, and I didn't know where to look for solutions once I ran into the problem.

zanieb commented 3 weeks ago

Addressing that with https://github.com/astral-sh/uv/pull/6454 — let me know if that's not helpful.

ashbywinch commented 3 weeks ago

That looks spot on 👍