Last active
March 3, 2025 23:57
-
-
Save mundstein/41cdef6267a91d87211ef010ba1c4f6a to your computer and use it in GitHub Desktop.
Conveniently activate python venv by looking for a venv directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// include this in your .zshrc or .bash_profile | |
activate () { | |
check_if_dir_is_venv () { | |
local dir="$1" | |
dir="$(realpath $dir)" | |
if [[ -d "${dir}/bin" && -d "${dir}/lib" && -f "${dir}/bin/activate" ]] | |
then | |
source "${dir}/bin/activate" | |
return 0 | |
fi | |
return 1 | |
} | |
check_dir_for_venv () { | |
local search_dir="$1" | |
for dir in "$search_dir"/*/ | |
do | |
check_if_dir_is_venv "$dir" && return 0 | |
done | |
check_if_dir_is_venv "$search_dir" && return 0 | |
return 1 | |
} | |
if check_dir_for_venv "." | |
then | |
return 0 | |
fi | |
if check_dir_for_venv ".." | |
then | |
return 0 | |
fi | |
echo "No virtual environment found." | |
return 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment