uv icon indicating copy to clipboard operation
uv copied to clipboard

Cannot `uv pip install` without virtualenv

Open hauntsaninja opened this issue 6 months ago • 5 comments

It's common to not use virtual environments in containers, e.g. see discussion around PEP 704. I don't mind the default being to prevent installation into a base environment, but it would be nice to have some low level flag to override it.

error: Failed to locate a virtualenv or Conda environment (checked: `VIRTUAL_ENV`, `CONDA_PREFIX`, and `.venv`). Run `uv venv` to create a virtualenv.

(Maybe I can lie about VIRTUAL_ENV to work around...)

hauntsaninja avatar Feb 15 '24 22:02 hauntsaninja

👍 Agreed, we need some solution to this. I believe setting VIRTUAL_ENV would work, but it should be a first-class feature.

charliermarsh avatar Feb 15 '24 22:02 charliermarsh

It would also be cool to do a couple of examples on how to create a Dockerfile

especially with multi-stage builds, as I don't want to carry all the dependencies (c-library) that are needed when building, previously I used --wheel-dir /app/wheels to do this

batazor avatar Feb 16 '24 23:02 batazor

Here's how I setup my Dockerfile to work until we get a feature in uv to specifically support this:

ENV VIRTUAL_ENV /usr/local/

RUN curl -LsSf https://astral.sh/uv/install.sh | sh \
    && . $HOME/.cargo/env \
    && uv pip compile requirements.in -o requirements.txt \
    && uv pip sync requirements.txt

/usr/local/ is correct on slim-bullseye images, you'll have to modify for whatever you image uses.

dmwyatt avatar Feb 17 '24 17:02 dmwyatt

Since this issue is somewhat popular, just noting that I've been doing VIRTUAL_ENV=$(python -c "import sys; print(sys.prefix)") uv pip ...

hauntsaninja avatar Feb 17 '24 21:02 hauntsaninja

This probably would be the ultimate fix https://github.com/astral-sh/uv/issues/1396

ofek avatar Feb 18 '24 23:02 ofek

pip has the --require-virtualenv option which makes it behave like uv:

$ pip install --require-virtualenv attrs
ERROR: Could not find an activated virtualenv (required).

but --no-require-virtualenv seems too long of a name for an option in uv. And sure the default uv behavior is much saner default!

ericbn avatar Feb 19 '24 14:02 ericbn

I think we can track this in #1396 and #1526 since those are concrete solutions.

Thanks everyone!

zanieb avatar Feb 19 '24 15:02 zanieb

I will mention the simplest solution, the --system option since other comments do not mention it. Also, the documentation highlights this option for containerized environments:

uv pip install --system

WARNING: --system is intended for use in continuous integration (CI) environments and should be used with caution, as it can modify the system Python installation.

SzilvasiPeter avatar Jul 09 '24 15:07 SzilvasiPeter