Created
October 16, 2025 09:01
-
-
Save Stfort52/b2b53219c04641342bb22a43a8163b33 to your computer and use it in GitHub Desktop.
Adds a function here to activate conda env matching your working directory and its parents. Useful if you make conda env names from project directory name.
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
| function init_conda_envs() { | |
| local env_dir="$HOME/path/to/your/conda/dir/envs" | |
| if [[ -d "$env_dir" ]]; then | |
| export CONDA_ENVS=$(ls -1 "$env_dir" | xargs) | |
| else | |
| export CONDA_ENVS="" | |
| fi | |
| } | |
| function here() { | |
| local dir=$(pwd) | |
| local envs=($CONDA_ENVS) | |
| while [[ "$dir" != "/" ]]; do | |
| local base=$(basename "$dir") | |
| for env in "${envs[@]}"; do | |
| if [[ "$base" == "$env" ]]; then | |
| echo "Activating environment: $env" | |
| conda activate "$env" | |
| return 0 | |
| fi | |
| done | |
| dir=$(dirname "$dir") | |
| done | |
| echo "No matching env found" | |
| return 1 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment