astral-sh / uv

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

`uv pip install` does not respect `UV_PROJECT_ENVIRONMENT`. Should it? #7380

Open nikhilweee opened 1 week ago

nikhilweee commented 1 week ago

When UV_PROJECT_ENVIRONMENT is unset, uv pip install <package> works without activating venv

$ uv venv
Using Python 3.10.12 interpreter at: /usr/bin/python3
Creating virtualenv at: .venv
Activate with: source .venv/bin/activate
$ uv pip install requests # no need to activate venv
Resolved 5 packages in 212ms
Installed 5 packages in 20ms
 + certifi==2024.8.30
 + charset-normalizer==3.3.2
 + idna==3.8
 + requests==2.32.3
 + urllib3==2.2.3

When UV_PROJECT_ENVIRONMENT is set, uv pip install <package> complains about not activating venv

$ export UV_PROJECT_ENVIRONMENT=venv
$ uv venv
Using Python 3.10.12 interpreter at: /usr/bin/python3
Creating virtualenv at: venv
Activate with: source venv/bin/activate
$ uv pip install requests # complains about not activating venv
error: No virtual environment found; run `uv venv` to create an environment, 
or pass `--system` to install into a non-virtual environment

This is with uv version 0.4.10

$ uv --version
uv 0.4.10

Should the behaviour be consistent for both cases? (I think so)

zanieb commented 1 week ago

UV_PROJECT_ENVIRONMENT is only for project-level commands, not the low-level pip interface. Are you trying to use this in a uv-managed project or an arbitrary directory?

nikhilweee commented 1 week ago

Thanks for the clarification. I'm trying to run this from an arbitrary directory. If this is intended please feel free to close the issue.

maxbrunet commented 1 day ago

In my case it is for an uv-managed project, I find it a little confusing that uv pip uses .venv by default without activation, but not UV_PROJECT_ENVIRONMENT

I use uv pip to work around packages that do not build in isolation, and this example from the doc does not work when UV_PROJECT_ENVIRONMENT is set:

$ export UV_PROJECT_ENVIRONMENT=/some/other/path/to/venv
$ uv venv
$ uv pip install torch
$ uv sync --no-build-isolation-package=flash-attn
error: No virtual environment found; run `uv venv` to create an environment, or pass `--system` to install into a non-virtual environment)

Instead one must do:

$ export UV_PROJECT_ENVIRONMENT=/some/other/path/to/venv
$ uv venv
$ VIRTUAL_ENV="${UV_PROJECT_ENVIRONMENT}" uv pip install torch
$ uv sync --no-build-isolation-package=flash-attn
zanieb commented 1 day ago

We could consider having uv pip respect UV_PROJECT_ENVIRONMENT if you are in a project root, but it's pretty complicated. I think what you're doing is a reasonable workaround. Ideally we'll have better ways to declare build dependencies in the long-run so you don't need to use uv pip.

maxbrunet commented 1 day ago

Ideally we'll have better ways to declare build dependencies in the long-run so you don't need to use uv pip.

I would love that! ❤️