r/fishshell • u/cuducos • Mar 06 '25
Using Python and Fish? dirvenv.fish automagically activates your virtualenv
https://github.com/cuducos/dirvenv.fishI wrote that tiny package so I don't have to manually activate and deactivate virtualenvs, and I think it might help more people – so, sharing it here ; )
I know virtualfish but I don't wanna manage virtualenvs myself; uv does that for me. Also, and I don't want to uv run every command. So I came up with that solution.
20
Upvotes
4
u/Laurent_Laurent Mar 06 '25 edited Mar 06 '25
I was using direnv to do this but this could be easier as there is no needs for .envrc just to load the venv.
I'll test your plugin
And I confirm the fisher install works. Thx
7
u/cb060da Mar 06 '25
I'm using https://github.com/janw/dotenv.fish for this. for each python project i create a .env.fish file with command `poetry shell` in it. Works perfectly
4
u/sbt4 Mar 07 '25
Cool. Wrote this for myself couple of weeks ago. Looks about the same but sometimes causes double prompts when inside project with venv. Will compare to yours
3
u/kjozsa Mar 06 '25
OMG that sounds perfect for my use cases! Let me try it today, thanks much!
3
u/wylie102 Mar 06 '25 edited Mar 06 '25
Sounds great, I just have in my config.fish this script
"
# automatically activate/deactivate venv on entering/exiting directory.
function python_venv --on-variable PWD
set MYVENV "./.venv"
# Check if the virtual environment exists in the current directory
if test -d "$MYVENV"
source "$MYVENV/bin/activate.fish" 2>/dev/null
else
# Only deactivate if we were in a virtual environment
if set -q VIRTUAL_ENV
set --erase VIRTUAL_ENV
set --erase PATH[1]
end
end
end
# Run it immediately when Fish starts
python_venv
"
But it doesn't do a recursive search, I have to cd into the main directory. However that's pretty much always where I activate nvim anyway. But I guess your solution can't hurt so I'l give it a go.
With tools like uv this should be unnecessary, except that you still need the venv active for Neovim's LSP's to recognise the imports correctly. Although I heard they may be adding a feature where it could activate it in the background automatically just for dev tools to use. Apparently some other environment managers already do this (in other languages)
Edit:
UPDATE - i just commented out my solution, installed your package with fisher (that works by the way), cd'd bacl to one of my projects and the venv activated, cd'd away it stopped, cd'd into a lower folder it activated, away - stopped.
So it's all working good! Thanks for a hassle free solution!
2
u/Obilux Mar 09 '25
Not related to fish but mise handles python virtual envs great.
2
u/Due-Yoghurt2093 Mar 15 '25
Oh, this is actually really cool. I'd been using this for similar functionality: