├── .gitignore ├── README.md └── bin └── pyenv-autoenv /.gitignore: -------------------------------------------------------------------------------- 1 | .*.swp 2 | .*.swo 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## install: 2 | 3 | 0-) You need to have **virtualenvwrapper** installed 4 | 5 | 1-) first install **pyenv**: 6 | 7 | git clone git://github.com/yyuu/pyenv.git ~/.pyenv 8 | 9 | 2-) then just: 10 | 11 | git clone git://github.com/andersoncardoso/pyenv-autoenv.git ~/.pyenv/plugins/pyenv-autoenv 12 | 13 | 3-) add these lines to your .bashrc or .zshrc: 14 | 15 | if [[ -d $HOME/.pyenv ]];then 16 | export PATH="$HOME/.pyenv/bin:$PATH" 17 | eval "$(pyenv init -)" 18 | source ~/.pyenv/plugins/pyenv-autoenv/bin/pyenv-autoenv 19 | fi 20 | 21 | ## How it works: 22 | 23 | Pyenv already has a behavior where: whenever you enter a directory with a `.python-version` file set 24 | with a version number in, he autoloads that python version. 25 | 26 | I've extended this behavior to work with virtualenvwrapper. So whenever you enter a dir with a `.python-env` 27 | set with a virtualenv name, he activates that environment automatically. 28 | 29 | Another aditional behavior is: If you don't have the python version from `.python-version` installed, 30 | he automatically installs it for you. And if you dont have the virtualenv from `.python-env` created, 31 | he creates and load it for you. 32 | TODO: make this optional 33 | 34 | Example: if you have a project on git with a `.python-version` with `2.7.5` in it, and a `.python-env` with `my_env`. 35 | When you clone it, all need to do is `cd` into the project's directory and it will load (and install if needed) both 36 | the python-2.7.5 and the my_env virtualenv (*uses virtualenvwrapper* so you need to have it installed). 37 | 38 | -------------------------------------------------------------------------------- /bin/pyenv-autoenv: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # virtualenv-auto-activate.sh 3 | # 4 | # Installation: 5 | # Add this line to your .bashrc or .bash-profile: 6 | # 7 | # source /path/to/virtualenv-auto-activate.sh 8 | # 9 | 10 | function _python_version_auto_install() { 11 | if [ -e ".python-version" ];then 12 | _PY_VERSION=$(cat .python-version) 13 | if [ "$_PY_VERSION" != "system" -a ! -d "$PYENV_ROOT/versions/$_PY_VERSION" ];then 14 | echo "Installing Python $_PY_VERSION" 15 | pyenv install $_PY_VERSION 16 | pyenv rehash 17 | fi 18 | 19 | fi 20 | } 21 | 22 | function _virtualenv_auto_activate() { 23 | if [ -e ".python-env" ]; then 24 | # Check for file containing name of virtualenv 25 | if [ -f ".python-env" ]; then 26 | _VENV_PATH=$WORKON_HOME/$(cat .python-env) 27 | else 28 | return 29 | fi 30 | 31 | # Check to see if already activated to avoid redundant activating 32 | if [ "$VIRTUAL_ENV" != $_VENV_PATH ]; then 33 | _VENV_NAME=$(basename $_VENV_PATH) 34 | # if venv does not exists create 35 | if [ -d "$_VENV_PATH" ];then 36 | workon $_VENV_NAME 37 | else 38 | echo "Virtualenv $_VENV_NAME does not exist. Adding with current python -> $(cat .python-version)" 39 | mkvirtualenv $_VENV_NAME --python $PYENV_ROOT/versions/$(cat .python-version)/bin/python 40 | workon $_VENV_NAME 41 | fi 42 | # echo Activated virtualenv \"$_VENV_NAME\". 43 | fi 44 | fi 45 | } 46 | 47 | function _autoenv_activate() { 48 | _python_version_auto_install 49 | _virtualenv_auto_activate 50 | } 51 | 52 | if [[ $PROMPT_COMMAND =~ ^.*\;[[:space:]$'\n'$'\r']*$ ]]; then 53 | export PROMPT_COMMAND="${PROMPT_COMMAND%;*}" 54 | fi 55 | export PROMPT_COMMAND="$([[ $PROMPT_COMMAND ]] && echo "${PROMPT_COMMAND}; ")_autoenv_activate" 56 | if [ -n "$ZSH_VERSION" ]; then 57 | function chpwd() { 58 | _autoenv_activate 59 | } 60 | fi 61 | --------------------------------------------------------------------------------