Skip to content
Stuff I'm Up To
Go back
Fish and Python autovenv

Fish and Python autovenv

I used a plugin for a while to automatically activate and deactivate my Python environments when I change directory. It looks like that’s been discontinued. (Fish and autovenv)

I took it upon myself to roll my own:

~/.config/fish/conf.d/autovenv.fish

function python_venv --on-variable PWD
    set -l tree (pwd)

    while test "$tree" != /
        for myvenv in "$tree/.venv" "$tree/venv"
            if test -d "$myvenv"
                if test "$PATH[1]" != "$myvenv/bin"
                    if functions -q deactivate
                        deactivate
                    end
                    source "$myvenv/bin/activate.fish"
                end
                return
            end
        end

        set tree (dirname "$tree")
    end

    if functions -q deactivate
        deactivate
    end
end

If it finds a folder called venv or .venv in the current or parent folder it will activate it.


Share this post:

Previous Post
Why I Moved My Biggest Project from React to Svelte
Next Post
11 Laws of the Universe