-
Notifications
You must be signed in to change notification settings - Fork 640
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
official support for uv #1250
Comments
I've been using this in my layout_python_uv() {
local python=${1:-python}
[[ $# -gt 0 ]] && shift
unset PYTHONHOME
local python_version=$($python -V | cut -w -f 2 | cut -d . -f 1-2)
if [[ -z $python_version ]]; then
log_error "Could not find python's version"
return 1
fi
if [[ -n "${VIRTUAL_ENV:-}" ]]; then
local REPLY
realpath.absolute "$VIRTUAL_ENV"
VIRTUAL_ENV=$REPLY
else
VIRTUAL_ENV=$(direnv_layout_dir)/python-$python_version
fi
if [[ ! -d $VIRTUAL_ENV ]]; then
uv venv -p $python "$@" "$VIRTUAL_ENV"
fi
export VIRTUAL_ENV
PATH_add "$VIRTUAL_ENV/bin"
} |
Couple of changes to run on linux: layout_python_uv() {
local python=${1:-python}
[[ $# -gt 0 ]] && shift
unset PYTHONHOME
local python_path=$(uv python find $python)
local python_version=$($python_path -V | cut -d' ' -f 2 | cut -d . -f 1-2)
if [[ -z $python_version ]]; then
log_error "Could not find python's version"
return 1
fi
if [[ -n "${VIRTUAL_ENV:-}" ]]; then
local REPLY
realpath.absolute "$VIRTUAL_ENV"
VIRTUAL_ENV=$REPLY
else
VIRTUAL_ENV=$(direnv_layout_dir)/python-$python_version
fi
export UV_PROJECT_ENVIRONMENT=$VIRTUAL_ENV
if [[ ! -d $VIRTUAL_ENV ]]; then
uv venv -p $python "$@" "$VIRTUAL_ENV"
fi
export VIRTUAL_ENV
PATH_add "$VIRTUAL_ENV/bin"
}
|
Would be good to add it to the wiki. If you believe the integration to be stable, sending a PR to add it to the stdlib would be fine as well. |
It has been added to the wiki by @doolio and @sephib and @consoull it seems. (Though https://github.com/direnv/direnv/wiki was not updated to add it to the list there). But would be great to add this to stdlib as I think it is quite obvious uv will take over more and more workflows. |
I am wondering if we all share a common understanding of what an official support for uv should look like? Here are some things that (IMHO) might be worth considering. These points were mostly triggered by looking at #1329, but it might just make sense to think about this more broadly and define some more concrete requirements for this issue?
I guess what I am advocating for here is letting |
At least for the time being, activating the virtual environment also allows IDEs and other tools to see the dependencies included in the virtual environment. I use NeoVim with Ruff and without an activated venv ruff won't see the dependencies. |
@duckpuppy I agree that activating the virtual environment should be a feature, albeit possibly an optional one. |
I still use and prefer the uv pip commands. I am uncomfortable with the idea of letting one tool (however rapid its development and adoption) take over the Python ecosystem. Also there are a variety of situations (using meson-python to build packages, for example) where activating the virtualenv is necessary. |
(see mesonbuild/meson-python#630 for more on that) |
Activating the virtual environment is to me the most important point that direnv should do. All the others are optional. I don't think it should sync automatically. I do think it should install the Python version specified automatically if it is not installed, or rather warn about it. Please note that often the version can only be specified in Ergo: It seems we have very different goals of what a default direnv implementation should do. To me activating the virtual environment seems the most reasonable one. |
As a just-ordinary-user of Direnv and uv, here's where I settled on (after watching Hynek on YouTube, of course), when in project mode: uv sync
source .venv/bin/activate I agree that's a little too blocking on first run, but it's working fine for me for now. A couple of notes after some fiddling:
So for project with lockfile I don't feel the need for a specific direnv support. That said some wrapping of |
for the record i do not ever ask uv to download some random python binary. i use pyenv's python-build script (which i have installed as a standalone binary), so my .envrc usually looks something like this (with the function definition given here):
or of course sometimes more like this:
|
While I would consider using a stdlib If
|
I feel it would be unwise to default to installing python versions via direnv - perhaps such behaviour could be an opt-in preference for those users/teams that want it? I can imagine plenty of users like @inklesspen that will use arbitrary, explicit python binaries by design. |
I'd like to also add that there's no need for a special A |
I've proposed a more project-centric integration via #1352. |
Uv has evolved significantly in 8 months.
Fwiw, I use |
|
That's why #1352 also activates the venv. |
Can I get a sense from the direnv team what the decision criteria is on these PRs? I'd like to know how much more time I should throw at it, and who's gonna be the decider for if it gets merged. |
Is your feature request related to a problem? Please describe.
I'd like direnv to layout python using astral-sh/uv in an elegant way.
Describe the solution you'd like
While this works, it breaks the prompt somehow.
By default, without using direnv, uv uses the name of the base directory for the venv prompt.
Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.
Additional context
Add any other context or screenshots about the feature request here.
The text was updated successfully, but these errors were encountered: