Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ashbywinch opened this issue Aug 21, 2024 · 5 comments

Comments

@ashbywinch
Copy link

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
Copy link
Member

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
Copy link
Member

zanieb commented Aug 21, 2024

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
Copy link
Author

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
Copy link
Member

zanieb commented Aug 22, 2024

Addressing that with #6454 — let me know if that's not helpful.

@zanieb zanieb closed this as completed Aug 22, 2024
@ashbywinch
Copy link
Author

That looks spot on 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants