Skip to content

Latest commit

 

History

History
91 lines (62 loc) · 3.39 KB

README.md

File metadata and controls

91 lines (62 loc) · 3.39 KB

CAVEZ / Conda Automatic Virtual Environments for Zsh

This is another tool that automatically switches a Python virtual environment based on the directory you are in. This tool only works if the following assumptions are met:

  • The virtual environment is stored in the project directory
  • The virtual environment directory is named either .venv or the name you specified in CAVEZ_VENV_DIR_NAME environment variable

This tool works with conda, mamba, and micromamba. You can specify the conda flavor in the CAVEZ_CONDA_FLAVOUR environment variable. If this variable is not set, the tool will try to automatically detect which flavor is installed.

Why another tool?

  • I use Micromamba implementation of Conda, which is rarely supported by other similar tools
  • I prefer to keep a virtual environment in the project directory, e.g. micromamba create -p ./.venv, which is rarely supported by other similar tools as well
  • This implementation is dead simple
  • It doesn't override cd, which I find clumsy

Dependencies

  • This is a plugin for Oh My Zsh, so the latter should be installed.
  • Any implementation of Conda should be installed, e.g. Conda itself, Mamba or Micromamba.

Installation

ZPlug

zplug "igrmk/cavez"

Antigen

antigen bundle "igrmk/cavez"

Zgen

zgen load "igrmk/cavez"

zinit

zinit wait lucid for igrmk/cavez

oh-my-zsh

Copy this repository to $ZSH_CUSTOM/plugins, where $ZSH_CUSTOM is the directory with custom plugins of oh-my-zsh (read more):

git clone "https://github.com/igrmk/cavez.git" "$ZSH_CUSTOM/plugins/cavez"

Then add "cavez" to your plugins. For example, add this line to your .zshrc. Make sure it is before the line source $ZSH/oh-my-zsh.sh.

plugins+=(cavez)

Manual Installation

Source the plugin shell script in your ~/.zshrc profile. For example

source $HOME/cavez/cavez.plugin.zsh

Thanks to Michael Aquilina for this project https://github.com/MichaelAquilina/zsh-autoswitch-virtualenv. I've just copied installation instructions from it.

Configuration

Environment variable Description
CAVEZ_VENV_DIR_NAME The name of a directory containing a virtual environment
CAVEZ_VERBOSE Report any actions performed by CAVEZ if the value equals "true"
CAVEZ_SKIP_HOOK_INIT Skip the initialization of the hook, this is only for debugging purposes

Powerlevel10k

If you are using the Powerlevel10k Zsh theme and want to show only the important part of the Conda prefix, you can update your ~/.p10k.zsh config file. Override the following variable to do it:

typeset -g POWERLEVEL9K_ANACONDA_CONTENT_EXPANSION='$(
  if [[ "${CONDA_DEFAULT_ENV}" == */.venv ]]; then
    basename "${CONDA_DEFAULT_ENV%/.venv}"
  else
    printf "%s" "${CONDA_DEFAULT_ENV}"
  fi
)'