Skip to content

Instantly share code, notes, and snippets.

@tommyip
Last active July 13, 2025 08:17
Show Gist options
  • Save tommyip/cf9099fa6053e30247e5d0318de2fb9e to your computer and use it in GitHub Desktop.
Save tommyip/cf9099fa6053e30247e5d0318de2fb9e to your computer and use it in GitHub Desktop.
venv.fish - Automatically activate/deactivate virtualenv in fish shell
# Based on https://gist.github.com/bastibe/c0950e463ffdfdfada7adf149ae77c6f
# Changes:
# * Instead of overriding cd, we detect directory change. This allows the script to work
# for other means of cd, such as z.
# * Update syntax to work with new versions of fish.
# * Handle virtualenvs that are not located in the root of a git directory.
function __auto_source_venv --on-variable PWD --description "Activate/Deactivate virtualenv on directory change"
status --is-command-substitution; and return
# Check if we are inside a git directory
if git rev-parse --show-toplevel &>/dev/null
set gitdir (realpath (git rev-parse --show-toplevel))
set cwd (pwd -P)
# While we are still inside the git directory, find the closest
# virtualenv starting from the current directory.
while string match "$gitdir*" "$cwd" &>/dev/null
if test -e "$cwd/.venv/bin/activate.fish"
source "$cwd/.venv/bin/activate.fish" &>/dev/null
return
else
set cwd (path dirname "$cwd")
end
end
end
# If virtualenv activated but we are not in a git directory, deactivate.
if test -n "$VIRTUAL_ENV"
deactivate
end
end
@perkfly
Copy link

perkfly commented Dec 4, 2021

This works great, Thanks.

@sqwxl
Copy link

sqwxl commented Nov 22, 2022

nice!

@Susensio
Copy link

Awesome!

@nakulj
Copy link

nakulj commented Jan 25, 2023

I have packaged this into a fisher plugin: https://github.com/nakulj/auto-venv

@sashaegorov
Copy link

Stunning!👍🏻

@rphes
Copy link

rphes commented Dec 6, 2023

Thanks for this!

If you call __auto_source_venv at the end of this script, your environment will also be activated when you open a shell in a Python project directory directly.

@tommyip
Copy link
Author

tommyip commented Jan 1, 2024

MacOS users - you might want to update to the latest version if your project resides in a location with uppercase directory names.

-    set cwd (pwd)
+    set cwd (pwd -P)

MacOS has a case insensitive file system where pwd (with the default -L/--logical flag) returns a path with lowercase directory names which causes issues with the case sensitive output of git --rev-parse.

@SpaceShaman
Copy link

I made a plugin called autopy.fish for the Fish Shell that automatically activates your Python virtual environment (venv or Poetry) when you cd into a project directory—and deactivates any previously active environment. It detects .venv or pyproject.toml, works with Git repos (uses the repo root as the project dir), and is designed to be fast and lightweight, running on every directory change. Install with Fisher or manually.
Check it out here: https://github.com/SpaceShaman/autopy.fish

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment