uv: `uv add`/`uv sync`/... do not respect the active virtualenv, should they?
Related to #1495, #5229, but not exactly the same IMO.
Like #1495, I prefer to have my virtualenvs located centrally instead of in project directories. I have a Fish shell function called v <NAME> that activates a virtualenv ~/envs/<NAME> if available and cds to the related project directory ~/build/<NAME> if available.
I wanted to try out uv add and uv sync and was initially somewhat confused when they didn’t seem to do anything, only to realize that they currently always work on the ~/.venv virtualenv, and don’t even stop to consider a currently active virtualenv to work on.
There’s apparently a workaround to symlink .venv to the centrally-located venv, but that also seems a bit messy too.
I think it should be at least an (envvar or config-file) option to have the “project venv” tools (run, add, remove, sync, etc.) consider the currently active virtualenv, if any.
Pretty minimal Docker container repro:
~ $ docker run -it python:3.12 bash
root@7ab1d7a6054d:/# pip install uv
Successfully installed uv-0.3.3
root@7ab1d7a6054d:/# mkdir ~/envs
root@7ab1d7a6054d:/# uv venv ~/envs/myenv
Using Python 3.12.4 interpreter at: usr/local/bin/python3
Creating virtualenv at: root/envs/myenv
root@7ab1d7a6054d:/# source ~/envs/myenv/bin/activate
(myenv) root@7ab1d7a6054d:/# echo $VIRTUAL_ENV
/root/envs/myenv
(myenv) root@7ab1d7a6054d:/# mkdir -p ~/projects/myproject
(myenv) root@7ab1d7a6054d:/# cd ~/projects/myproject
(myenv) root@7ab1d7a6054d:~/projects/myproject# uv init
Initialized project `myproject`
(myenv) root@7ab1d7a6054d:~/projects/myproject# uv add ruff
Using Python 3.12.4 interpreter at: /usr/local/bin/python3
Creating virtualenv at: .venv
Resolved 2 packages in 275ms
Built myproject @ file:///root/projects/myproject
Prepared 2 packages in 983ms
Installed 2 packages in 1ms
+ myproject==0.1.0 (from file:///root/projects/myproject)
+ ruff==0.6.2
(myenv) root@7ab1d7a6054d:~/projects/myproject#
(myenv) root@7ab1d7a6054d:~/projects/myproject# uv pip list
(myenv) root@7ab1d7a6054d:~/projects/myproject#
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 8
- Comments: 48 (17 by maintainers)
It seems kind of wild to not respect VIRTUAL_ENV, for any command honestly. I’m not sure I can imagine a drawback. in the happy path, no one is setting it, or otherwise activating the virtualenv. but in every other case, it’s the path of least surprise to me.
I frequently use quick swapping of virutalenvs under different names to fast test across multiple versions of python. I personally do so with my own tool (currently, would love to stop using it)
prp -n p39; pytest,prp -n p310; pytest, where my venvs live somewhere else entirely…Granted withuv,uv sync --python 3.9takes all of 1s to swap, it does unnecessarily kill and recreate the venv each time, and output 2 pages of text.Somewhat tangentially related, I submitted https://github.com/astral-sh/uv/issues/1495 a while back basically to facilitate this workflow. Where ideally i wouldn’t even necessarily be activating any of the venvs, if i could be doing something like
uv -p 3.9 sync,uv -p 3.9 run pytest(then swap to-p 3.10); and if under the hood it was just reading/writing to different venv paths external to the project folder (ideally based onpwdand the python version, since./.venvclobbers itself, and any other local path starts creating clutter). Then in that world, I might no longer have a need or reason to activate a venv in the first place.I would prefer it if e.g.
uv syncwould respectVIRTUAL_ENV.It was the most surprising thing I found when starting to use
uv- I commonly have active virtual environments that I want to be obeyed byuvcommands. I think the fact that the environment is active is good indication that the user intends to use it and if they don’t, that’s a user error.It also is a surprising difference from
uv pip install, which does respectVIRTUAL_ENV. I think it would be great ifuv synchad similar/consistent behavior.This is confusing to me, If I do:
Shouldnt the
uv syncrespect that I created the venv in the first step, it does not seem to do so. What is the use case foruv venv, justuv pip?@rickyzhang82 that’s not a productive or acceptable way to engage with this project. I’m sorry this is confusing for you, but if you want it to be changed you need to contribute something new to the discussion.
It’s very confusing to me why
uv syncdoesn’t respect theVIRTUAL_ENVenvironment variable. Even according to the docs, it should be respecting it.The following should just work. I would expect this to create a new virtualenv in
/opt/venv, if one doesn’t exist and then install all of the projects dependencies.At the very lest the docs should be updated to explain what commands respect the
VIRTUAL_ENVenvironment variable and why others don’t.In the Docker context, I don’t really want to be forced into creating my virtualenv in
.venv.i mean, it’s ultimately up to them, but it seems weird to me for the project (versus myself or the tool) to have control over where my venv goes, regardless of the usecase. it totally makes sense for uv to have strong opinions or for the user. but i just really dont see a reason for the project itself to care (particularly with a tool like
uv, where you can write your project tooling to useuv runor whatever and expect it to do the right thing).This would be very helpful for us, too.
Our use case is that we run tox in our pipeline and the first step is to verify and install the requirements for the unit tests. So basically we would like to do
uv sync --extra test --lockedwith the current tox virtualenv being the target of the sync.@zanieb I agree it was not really respectful of the project, but I cant help but say this is truly my biggest pet peeves. Possibly one of the easiest things to fix to improve quality of life.
Working with multiple python versions at the same time make it weird with it deleting my venv and creating a new. Specially when I am not fully commited to a lib and have it just installed with
uv pipfor testing purposes. My IDE goes crazy because it does not re-fetch the python version.Of course all this can be fixed by changing how people do things but it is indeed a painful transition. The only argument I see is the one above:
I mean destroying and creating a .venv just because is the conventional name is way worse…
.venvis really just a convention and there is nothing requiring it to be the rule. The only formal mention I found was the now Withdrawn PEP 704.The env variable solution has yet another big issue which is to break under relative paths and it is annoying to set env variables for dev environments on cross platform teams.
If there is room for reconsideration, I also would be in favor. Maybe on another issue?
@rickyzhang82 it sounds like you want
uv pip sync pyproject.tomlinstead? If you’re using an environment-centric workflow then theuv pipinterface is for you. The top-levelsyncinterface is project-centric and is not designed to share environments across projects.This doesn’t make sense to me. Feel free to open an issue with the specifics, we prioritize bug fixes.
This is the pain in the a**. Why not respect env
VIRTUAL_ENVand use the new oneUV_PROJECT_ENVIRONMENT. The tool supposed to improve our productivity. But now everyone has to pay a toll to figure this out. It makes no sense to me.Yes, that would be sufficient for our use case. Thanks a lot for your work!
I don’t know if anyone got my suggestion above, but it would be great if there was a config in pyproject.toml to refer to the virtual env path besides the VIRTUAL_ENV variable (of course an active env would take priority). This makes it possible to run uv sync/add as intended without activating the env manually.
or
I’m very eager to use this new add/sync interface! Thank you guys for such cool project
Just a brief drive-by comment: this is something we’ve considered but cut from scope for last week’s release 😃
Can you explain the reasoning behind this? I thought most users activate a virtual environment to ensure they’re not modifying anything globally.
similar to @akx I keep my virtual environments outside of the repo to save space. Then have an alias to activate whichever environment I need or want to test. It would be nice to have an
--active-envflag that can be used withsync/add/… commands, or set some config, to allow for this.ok, let me adapt that example in a fork, and push it in a few
Yep! We agree the Docker volume mount is a big problem and we are working on designing a recommended solution.
Does adding
.venvto.dockerignorenot work?Yeah,
uv venvisn’t really for use with the project interface. It’s for manual management of virtual environments, which is generally the purview ofuv pip.