-
-
Save chusiang/f02a62ab23e2a355b845 to your computer and use it in GitHub Desktop.
virtualenv-auto-activate-auto-deactivate
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
#!/bin/bash | |
# virtualenv-auto-activate.sh | |
# | |
# Installation: | |
# Add this line to your .bashrc or .bash-profile: | |
# | |
# source /path/to/virtualenv-auto-activate.sh | |
# | |
# Go to your project folder, run "virtualenv .venv", so your project folder | |
# has a .venv folder at the top level, next to your version control directory. | |
# For example: | |
# . | |
# ├── .git | |
# │ ├── HEAD | |
# │ ├── config | |
# │ ├── description | |
# │ ├── hooks | |
# │ ├── info | |
# │ ├── objects | |
# │ └── refs | |
# └── .venv | |
# ├── bin | |
# ├── include | |
# └── lib | |
# | |
# The virtualenv will be activated automatically when you enter the directory. | |
_virtualenv_auto_activate() { | |
if [ -e ".venv" ]; then | |
# Check to see if already activated to avoid redundant activating | |
if [ "$VIRTUAL_ENV" = "" ]; then | |
_VENV_NAME=$(basename `pwd`) | |
echo Activating virtualenv \"$_VENV_NAME\"... | |
VIRTUAL_ENV_DISABLE_PROMPT=1 | |
source .venv/bin/activate | |
_OLD_VIRTUAL_PS1="$PS1" | |
PS1="($_VENV_NAME)$PS1" | |
export PS1 | |
fi | |
else | |
if [ "$VIRTUAL_ENV" != "" ]; then | |
echo deactivating VirtualEnv | |
deactivate | |
fi | |
fi | |
} | |
export PROMPT_COMMAND=_virtualenv_auto_activate |
-
Automatically activate virtualenv - Nathan Cahill: Simple, but it's better this for me.
$ vim ~/.bashrc ... function cd { builtin cd "$@" if [ -d "venv" ] ; then source venv/bin/activate fi }
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
進入擁有 .venv/ 的目錄時將會自行啟用 (activate) virtualenv,且離開目錄時將會自動離開 (deactivate) 此 virtualenv。