uv
uv copied to clipboard
Cannot `uv pip install` without virtualenv
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...)
👍 Agreed, we need some solution to this. I believe setting VIRTUAL_ENV
would work, but it should be a first-class feature.
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
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.
Since this issue is somewhat popular, just noting that I've been doing VIRTUAL_ENV=$(python -c "import sys; print(sys.prefix)") uv pip ...
This probably would be the ultimate fix https://github.com/astral-sh/uv/issues/1396
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!
I think we can track this in #1396 and #1526 since those are concrete solutions.
Thanks everyone!
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.