Replies: 2 comments 1 reply
-
Hey, thanks for asking about this. It's just Andy, btw, but I see how the username makes that unclear. I haven't used direnv but will give it a shot to try this out. I have used zsh-autoenv, which is much more straightforward for this case, as it runs any Zsh you want in the current shell, whereas direnv runs Bash and is mostly for exporting variables. So if you're using zsh-autoenv, you can have
a8 ${0:h}
if [[ $VIRTUAL_ENV ]] envout The zero in Now looking at direnv... layout python has direnv create a new venv, which is probably not what we want (at least not where and how it does so). I guess to do things the direnv way, we want to create a new So I'm looking at this example in the docs for a custom layout_uv() {
if [[ -d ".venv" ]]; then
VIRTUAL_ENV="$(pwd)/.venv"
fi
if [[ -z $VIRTUAL_ENV || ! -d $VIRTUAL_ENV ]]; then
log_status "No virtual environment exists. Executing \`uv venv\` to create one."
uv venv
VIRTUAL_ENV="$(pwd)/.venv"
fi
PATH_add "$VIRTUAL_ENV/bin"
export UV_ACTIVE=1 # or VENV_ACTIVE=1
export VIRTUAL_ENV
} There are a few ways we could go about this, either recreating logic from zpy, or invoking Zsh+zpy from Bash. I'd lean toward the latter approach for now. Ultimately I think there are these concrete goals for the function:
But I don't know how this is all undone cleanly, as it's not using venv's usual Let's see what happens anyway. zpy has a helpful subcommand $ zpy mkbin --help
Make a standalone script for any zpy function.
zpy mkbin <func> <dest>
$ zpy mkbin a8 ~/.local/bin/
$ zpy mkbin venvs_path ~/.local/bin/ Now I'll write a Bash function based on the above layout_zpy () {
a8
VIRTUAL_ENV="$(venvs_path)/venv"
PATH_add "$VIRTUAL_ENV/bin"
export VENV_ACTIVE=1
export VIRTUAL_ENV
} OK so now I'll try adding a new $ print 'layout zpy' >.envrc
direnv: error /home/andy/Code/nestedtextto/.envrc is blocked. Run `direnv allow` to approve its content
$ direnv allow
direnv: loading ~/Code/nestedtextto/.envrc
direnv: export +VENV_ACTIVE +VIRTUAL_ENV ~PATH
$ which python
/home/andy/.local/share/venvs/1be7b478d580cac73a8bca0e01d57a97/venv/bin/python
$ cd ..
direnv: unloading
$ which python
/home/andy/.local/share/mise/installs/python/latest/bin/python Looking good! And at least with, for example, p10k prompt for Zsh, the venv will be reflected there automatically. |
Beta Was this translation helpful? Give feedback.
-
Hi Andy, Thank you for the solution, its working great! I should have included my attempt using Best regards, |
Beta Was this translation helpful? Give feedback.
-
Hi Andyde,
Hope you are doing good! Recently I learned about
layout python
from direnv.I tried to figure out how to combine it with zpy, but using for example the
eval a8 --py current
would not pick up the direnv venv. Do you know if this would be possible?Beta Was this translation helpful? Give feedback.
All reactions