Skip to main content

Activate a virtualenv in a non-Git directory

ID
2d8e770
date
2024-01-03 13:13:19+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
79551f5
message
Activate a virtualenv in a non-Git directory
changed files
1 file, 16 additions, 5 deletions

Changed files

fish_functions/auto_activate_venv.fish (1363) → fish_functions/auto_activate_venv.fish (1688)

diff --git a/fish_functions/auto_activate_venv.fish b/fish_functions/auto_activate_venv.fish
index f2f1fd5..26de318 100644
--- a/fish_functions/auto_activate_venv.fish
+++ b/fish_functions/auto_activate_venv.fish
@@ -12,15 +12,26 @@ function auto_activate_venv --description "Auto activate/deactivate virtualenv w
 		# Get the top-level directory of the current Git repo (if any)
     set REPO_ROOT (git rev-parse --show-toplevel 2>/dev/null)
 
-    # Case #1: cd'd from a Git repo to a non-Git folder
+    # Case #1: cd'd into a folder which has a .venv in its root.
     #
-    # There's no virtualenv to activate, and we want to deactivate any
-    # virtualenv which is already active.
+    # Activate this virtualenv if it's not already activated.
+    if test -z "$REPO_ROOT"; and test -d "$(pwd)/.venv"
+        if [ "$VIRTUAL_ENV" != "$(pwd)/.venv" ]
+            source "$(pwd)/.venv/bin/activate.fish" &>/dev/null
+        end
+
+        return
+    end
+
+    # Case #2: cd'd to a folder which isn't a Git repo and doesn't
+    # have a .venv at its root.
+    #
+    # Deactivate any virtualenv which is already active.
     if test -z "$REPO_ROOT"; and test -n "$VIRTUAL_ENV"
         deactivate
     end
 
-    # Case #2: cd'd folders within the same Git repo
+    # Case #3: cd'd folders within the same Git repo
     #
     # The virtualenv for this Git repo is already activated, so there's
     # nothing more to do.
@@ -28,7 +39,7 @@ function auto_activate_venv --description "Auto activate/deactivate virtualenv w
         return
     end
 
-    # Case #3: cd'd from a non-Git folder into a Git repo
+    # Case #4: cd'd from a non-Git folder into a Git repo
     #
     # If there's a virtualenv in the root of this repo, we should
     # activate it now.