Skip to content

Automatically detect when uv is being used #18971

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

Open
godalming123 opened this issue Apr 26, 2025 · 5 comments
Open

Automatically detect when uv is being used #18971

godalming123 opened this issue Apr 26, 2025 · 5 comments

Comments

@godalming123
Copy link

Feature

If .venv/bin/python exists in the root of the project (where the pyproject.toml file is), then automatically use it as the python executable with priority over /usr/bin/python.

Pitch

This feature would make uv work out of the box, instead of a user having to figure out that they need to add --python-executable=.venv/bin/python to the mypy command that they run.

Alternative solutions

There could also be a help note if .venv/bin/python exists and mypy cannot find implementations or library stubs for the majority of the imports, that suggests adding --python-executable=.venv/bin/python to the mypy command.

@sterliakov
Copy link
Collaborator

Usually you have mypy installed in the same virtual environment, why are you running system installation of mypy against a codebase in venv? --python-executable exists to support that scenario, but it should be vanishingly rare. Given that mypy changes significantly with every release, relying on system installation is just begging for trouble, different projects may depend on different mypy versions.

@godalming123
Copy link
Author

How am I meant to use mypy with uv, since the documentation only explains how to use mypy with pip? Running uvx mypy, still can't find any of the imports to external libraries unless you add --python-executable=.venv/bin/python to the command. I'm happy to update the documentation to add instructions for using mypy with uv.

@brianschubert
Copy link
Collaborator

Using uv vs pip (or uvx vs pipx) doesn't actually matter per-se. What matters is whether mypy is running in the same virtual environment as where your project / its dependencies are installed. If you run mypy using uxv mypy, mypy will be installed in an isolated virtual environment that does not include your project's dependencies. If you instead install mypy into your project's virtual enviroment (e.g. with uv venv; uv pip install mypy), things should work as expected.

Also related: #17214

@A5rocks
Copy link
Collaborator

A5rocks commented Apr 27, 2025

Note that uvx will use a tool in the relevant venv if it's installed. You just need to add mypy as a dev dependency for the relevant project.

Nevermind I somehow confused uv tool run (=uvx) and uv run. Worse yet, in JS-land npx has above behavior :/

@hauntsaninja
Copy link
Collaborator

I wouldn't want us to automatically use .venv/bin/python. Adding the suggestion as a "note" additional diagnostic on import not found errors similar to #17214 could be good though!

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

No branches or pull requests

5 participants