Skip to content

Instantly share code, notes, and snippets.

@jaredyam
Last active October 26, 2020 13:30
Show Gist options
  • Select an option

  • Save jaredyam/b8e987664efeb98261247b215f1716be to your computer and use it in GitHub Desktop.

Select an option

Save jaredyam/b8e987664efeb98261247b215f1716be to your computer and use it in GitHub Desktop.
Wrap up the project environment activate/deactivate methods in the cd command.
# cd-with-auto-project-env.sh
#
# Wrap up the project environment activate/deactivate methods in the cd command.
#
# Custom
# ------
# 1. env : environment directory contained in the project root.
# 2. conda activate ./env : method to activate the target environment.
# 3. conda deactivate : method to deactivate the current environment.
# Prerequisite
# ------------
# The system variable $PYTHONPATH should have been set (or unset)
# when you're in (or out of) the project directory.
# Recommend to use the `direnv` tool for this purpose.
cd() {
builtin cd "$1"
if [ -n "$PYTHONPATH" ]; then
conda activate "$PYTHONPATH"/env
__conda_activated="True"
elif [ -n "$__conda_activated" ] && [ -z "$PYTHONPATH" ]; then
conda deactivate
unset __conda_activated
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment