venv

venv makes working with Virtualenv, Virtualenvwrapper, Bash, ZSH, Vim, and IPython within a project context very easy.

There are a few parts to venv:

To configure a shell, print relevant env variables (twice), cd to the working directory ($_WRD), and open README.rst in a named Vim server for an existing venv named dotfiles:

we dotfiles; ds; cdwrd; e README.rst

Note

For a new venv (as might be created with mkvirtualenv dotfiles), the $_SRC and $_WRD directories do not yet exist. You can create these like so:

## _APP=dotfiles _WRD=$VIRTUAL_ENV/src/$_APP
mkdir -p $_WRD

Or, to create a more complete Filesystem Hierarchy Standard tree of directories under $VIRTUAL_ENV:

venv_mkdirs; mkdir -p $_WRD

Working With Venv

To work on a venv:

we dotfiles

we is an alias for workon_venv, which does source <(venv.py -q --print-bash --ve=dotfiles).

Verbosely:

__WRK="~/-wrk"                    # cdwrk
PROJECT_HOME="${__WRK}"           # cdph cdprojecthome
WORKON_HOME="${__WRK}/-ve27"      # cdwh cdworkonhome
__DOTFILES="~/-dotfiles"          # cdd  cddotfiles
                                  # a very special symlink to
                                  # $WORKON_HOME/dotfiles/src/dotfiles

__VENV=$(which venv.py);
__VENV="${__DOTFILES}/scripts/venv.py"  # 10-bashrc.venv.sh
# venv()  { (set -x; $__VENV $@)    }   # 10-bashrc.venv.sh
# venv-() { (set -x; $__VENV -e $@) }   # 10-bashrc.venv.sh

# These all print venv.py --help:
$__VENV --help
venv.py --help
venv -h

# Print environment variables for the "dotfiles" venv:
venv.py --print-vars --VIRTUAL_ENV="${WORKON_HOME}/dotfiles"
venv.py --print-vars --virtual-env="${WORKON_HOME}/dotfiles"
venv.py --print-vars --ve="${WORKON_HOME}/dotfiles"
venv.py --print-vars --ve dotfiles
venv.py --print-vars dotfiles

# Generate a source-able Bash configuration script
venv.py --print-bash dotfiles

## Workon the dotfiles virtualenv (in the current shell)
we dotfiles

## Workon the dotfiles virtualenv (with a bash subshell)
venv.py -x bash dotfiles
venv.py -xb dotfiles

Note

The following commands are different to argparse (argument order matter with positional arguments)

venv dotfiles --print-bash        # does not work
venv --print-bash dotfiles        # does work

# As a workaround, be explicit
venv --ve=dotfiles --print-bash   # does work

CdAlias

Virtualenv paths can be really long.

CdAliases make it easy to jump around to venv defined variables (like $_WRK (cdwrk), $WORKON_HOME (cdwh), VIRTUAL_ENV (cdv), $_SRC (cds), and $_WRD (cdw)).

Each CdAlias defined in env.aliases is expanded for Bash, IPython, and Vim. For example, CdAlias('__WRD') is expanded to cdwrd, cdw; %cdwrd, cdw, and :Cdwrk, :Cdw:

# Bash
cdwrd
cdwrd<tab>
cdwrd docs/
cdw docs/

# IPython
%cdwrd
cdwrd
cdwrd docs/
cdw docs/

# Vim
:Cdwrk
:Cdwrk docs/
:Cdw docs/

At build time, the dotfiles Makefile generates the venv CdAlias scripts like so:

# Generate venv CdAlias scripts
venv.py --print-bash-cdalias . | tee venv_cdalias.sh
venv.py --print-ipython-cdalias . | tee venv_cdmagic.py
venv.py --print-vim . | tee venv.vim

Subsequently, cdhelp displays:

## venv.sh
# generated from $(venv --print-bash --prefix=/)
    # cdhome            -- cd $HOME /$@
    # cdh               -- cd $HOME
    # cdwrk             -- cd $__WRK /$@
    # cddotfiles        -- cd $__DOTFILES /$@
    # cdd               -- cd $__DOTFILES
    # cdprojecthome     -- cd $PROJECT_HOME /$@
    # cdp               -- cd $PROJECT_HOME
    # cdph              -- cd $PROJECT_HOME
    # cdworkonhome      -- cd $WORKON_HOME /$@
    # cdwh              -- cd $WORKON_HOME
    # cdve              -- cd $WORKON_HOME
    # cdcondaenvspath   -- cd $CONDA_ENVS_PATH /$@
    # cda               -- cd $CONDA_ENVS_PATH
    # cdce              -- cd $CONDA_ENVS_PATH
    # cdvirtualenv      -- cd $VIRTUAL_ENV /$@
    # cdv               -- cd $VIRTUAL_ENV
    # cdsrc             -- cd $_SRC /$@
    # cds               -- cd $_SRC
    # cdwrd             -- cd $_WRD /$@
    # cdw               -- cd $_WRD
    # cdbin             -- cd $_BIN /$@
    # cdb               -- cd $_BIN
    # cdetc             -- cd $_ETC /$@
    # cde               -- cd $_ETC
    # cdlib             -- cd $_LIB /$@
    # cdl               -- cd $_LIB
    # cdlog             -- cd $_LOG /$@
    # cdpylib           -- cd $_PYLIB /$@
    # cdpysite          -- cd $_PYSITE /$@
    # cdsitepackages    -- cd $_PYSITE
    # cdvar             -- cd $_VAR /$@
    # cdwww             -- cd $_WWW /$@
    # cdww              -- cd $_WWW

venv.py

$ python ../src/dotfiles/venv/venv_ipyconfig.py --help
usage: venv [-h] [-V] [-v] [-D] [-T] [-q] [-t] [--platform PLATFORM] [-e]
            [--__WRK [__WRK]] [--__DOTFILES [__DOTFILES]]
            [--WORKON_HOME [WORKON_HOME]] [--VENVSTR [VENVSTR_]]
            [--VENVSTRAPP [VENVSTRAPP_]]
            [--VIRTUAL_ENV_NAME [VIRTUAL_ENV_NAME]]
            [--VENVPREFIX [VENVPREFIX]] [--VIRTUAL_ENV [VIRTUAL_ENV]]
            [--_SRC [_SRC]] [--_APP [_APP]] [--_WRD [_WRD]] [--print-json]
            [--print-json-filename [PRINT_JSON_FILENAME]] [--print-vars]
            [--print-bash] [--print-bash-all] [--print-bash-aliases]
            [--print-bash-cdaliases] [-Z] [--print-vim-cdalias]
            [--print-ipython-magics] [--command RUN_COMMAND] [--run-bash]
            [--run-make] [--run-editp] [--run-terminal] [--pall] [--pwrk]
            [--pworkonhome] [--pvirtualenv] [--psrc] [--pwrd] [--pdotfiles]
            [--prel] [--pkg-resource-path] [--compress]
            [VENVSTR] [VENVSTRAPP] ...

venv is a configuration utility for virtual environments.

positional arguments:
  VENVSTR               a name of a virtualenv in WORKON_HOME OR a full path
                        to a VIRTUAL_ENV
  VENVSTRAPP            a path within _SRC (_WRD=_SRC/VENVSTRAPP)
  args

optional arguments:
  -h, --help            show this help message and exit
  -V, --version
  -v, --verbose
  -D, --diff, --show-diffs
  -T, --trace
  -q, --quiet
  -t, --test
  --platform PLATFORM   Platform string (default: None)
  -e, --from-environ    Build venv.env.environ from keys in os.environ
  --__WRK [__WRK], --WRK [__WRK], --wrk [__WRK]
                        Path to workspace -- ~/-wrk
  --__DOTFILES [__DOTFILES], --DOTFILES [__DOTFILES], --dotfiles [__DOTFILES]
                        Path to ${__DOTFILES} symlink -- ~/-dotfiles
  --WORKON_HOME [WORKON_HOME], --workonhome [WORKON_HOME], --wh [WORKON_HOME]
                        Path to ${WORKON_HOME} directory containing
                        VIRTUAL_ENVs
  --VENVSTR [VENVSTR_], --venvstr [VENVSTR_], --ve [VENVSTR_]
                        Path to VIRTUAL_ENV --
                        ${WORKON_HOME}/${VIRTUAL_ENV_NAME} (or a dirname in
                        $WORKON_HOME)
  --VENVSTRAPP [VENVSTRAPP_], --venvstrapp [VENVSTRAPP_]
                        Subpath within {VIRTUAL_ETC}/src/
  --VIRTUAL_ENV_NAME [VIRTUAL_ENV_NAME], --virtual-env-name [VIRTUAL_ENV_NAME], --vename [VIRTUAL_ENV_NAME]
                        dirname in WORKON_HOME --
                        ${WORKON_HOME}/${VIRTUAL_ENV_NAME}
  --VENVPREFIX [VENVPREFIX], --venvprefix [VENVPREFIX], --prefix [VENVPREFIX]
                        Prefix for _SRC, _ETC, _WRD if [ -z VIRTUAL_ENV ]
  --VIRTUAL_ENV [VIRTUAL_ENV], --virtual-env [VIRTUAL_ENV]
                        Path to a $VIRTUAL_ENV
  --_SRC [_SRC], --SRC [_SRC], --src [_SRC]
                        Path to source -- ${VIRTUAL_ENV}/src")
  --_APP [_APP], --APP [_APP], --app [_APP]
                        Path component string -- ${_SRC}/${_APP}
  --_WRD [_WRD], --WRD [_WRD], --wrd [_WRD]
                        Path to working directory -- ${_SRC}/${_APP}
  --print-json          Print venv configuration as JSON
  --print-json-filename [PRINT_JSON_FILENAME]
                        Path to write venv env JSON to
  --print-vars, --vars  Print vars
  --print-bash, --bash  Print Bash shell configuration
  --print-bash-all      Print Bash shell environ and aliases
  --print-bash-aliases, --bash-alias
                        Print Bash alias script
  --print-bash-cdaliases, --bash-cdalias
                        Print Bash cdalias script
  -Z, --print-zsh       Print ZSH shell configuration
  --print-vim-cdalias, --vim
                        Print vimscript configuration
  --print-ipython-magics
                        Print IPython magic methods
  --command RUN_COMMAND, --cmd RUN_COMMAND, -x RUN_COMMAND
                        Run a command in a venv-configured shell
  --run-bash, --xbash, -xb
                        Run bash in the specified venv
  --run-make, --xmake, -xmake
                        Run (cd $_WRD; make $@) in the specified venv
  --run-editp, --open-editors, --edit, -E
                        Open $EDITOR_ with venv.project_files [$PROJECT_FILES]
  --run-terminal, --open-terminals, --terminals, --terms
                        Open terminals within the venv [gnome-terminal]
  --pall, --pathall     Print possible paths for the given path
  --pwrk, --wrk-path    Print $__WRK/$@
  --pworkonhome, --workonhome-path, --pwh
                        Print $__WORKON_HOME/$@
  --pvirtualenv, --virtualenv-path, --pv
                        Print $VIRTUAL_ENV/${@}
  --psrc, --src-path, --ps
                        Print $_SRC/${@}
  --pwrd, --wrd-path, --pw
                        Print $_WRD/${@}
  --pdotfiles, --dotfiles-path, --pd
                        Print ${__DOTFILES}/${path}
  --prel, --relative-path
                        Print ${@}
  --pkg-resource-path   Path from pkg_resources.TODOTODO
  --compress, --compress-paths
                        Path $VAR-ify the given paths from stdin

argparse.REMAINDER: If args must be specified, either (VENVSTR AND VENVSTRAPP)
or (--ve [--app]) must be specified first: venv --ve dotfiles -xmake.

Python API

A dotfiles.venv.venv_ipyconfig.Venv object builds:

A dotfiles.venv.venv_ipyconfig.Venv object can then be serialized:

  • --print-vars – easy to read variables
  • --print-json – quoted and escaped JSON
  • --print-bash – quoted and escaped shell script
  • IPython %alias configuration dict (see %alias?)

There are a number of unittest.TestCase tests in dotfiles.venv.venv_ipyconfig (venv_ipyconfig.py) for each of the build steps.

venv --verbose --show-diffs shows what is going on.

Example Venv Configuration

Shell Configuration

venv.py --print-bash --compress dotfilesx dotfilesx/docs:

$ python ../scripts/venv.py --print-bash --compress dotfilesx dotfilesx/docs \
| sed "s,${HOME},~,g"
export HOME="~"
export __WRK="${HOME}/-wrk"
export __SRC="${PROJECT_HOME}/-src"
export __DOTFILES="${HOME}/-dotfiles"
export PROJECT_HOME="${HOME}/-wrk"
export WORKON_HOME__py27="${PROJECT_HOME}/-ve27"
export WORKON_HOME__py34="${PROJECT_HOME}/-ve34"
export WORKON_HOME_DEFAULT="WORKON_HOME__py27"
export CONDA_ROOT__py27="${PROJECT_HOME}/-conda27"
export CONDA_ENVS__py27="${PROJECT_HOME}/-ce27"
export CONDA_ROOT__py34="${PROJECT_HOME}/-conda34"
export CONDA_ENVS__py34="${PROJECT_HOME}/-ce34"
export CONDA_ROOT="${PROJECT_HOME}/-conda27"
export CONDA_ENVS_PATH="${PROJECT_HOME}/-ce27"
export WORKON_HOME="${PROJECT_HOME}/-ve27"
export VENVSTR="dotfilesx"
export VENVSTRAPP="${VENVSTR}/docs"
export VIRTUAL_ENV_NAME="${VENVSTR}"
export VIRTUAL_ENV="${WORKON_HOME}/${VENVSTR}"
export VENVPREFIX="${WORKON_HOME}/${VENVSTR}"
export _APP="${VENVSTRAPP}"
export _ETC="${VIRTUAL_ENV}/etc"
export _SRC="${VIRTUAL_ENV}/src"
export _WRD="${_SRC}/${VENVSTRAPP}"
export _BIN="${VIRTUAL_ENV}/bin"
export _ETCOPT="${_ETC}/opt"
export _HOME="${VIRTUAL_ENV}/home"
export _LIB="${VIRTUAL_ENV}/lib"
export _PYLIB="${VIRTUAL_ENV}/lib/python2.7"
export _PYSITE="${VIRTUAL_ENV}/lib/python2.7/site-packages"
export _MNT="${VIRTUAL_ENV}/mnt"
export _MEDIA="${VIRTUAL_ENV}/media"
export _OPT="${VIRTUAL_ENV}/opt"
export _ROOT="${VIRTUAL_ENV}/root"
export _SBIN="${VIRTUAL_ENV}/sbin"
export _SRV="${VIRTUAL_ENV}/srv"
export _TMP="${VIRTUAL_ENV}/tmp"
export _USR="${VIRTUAL_ENV}/usr"
export _USRBIN="${VIRTUAL_ENV}/usr/bin"
export _USRINCLUDE="${VIRTUAL_ENV}/usr/include"
export _USRLIB="${VIRTUAL_ENV}/usr/lib"
export _USRLOCAL="${VIRTUAL_ENV}/usr/local"
export _USRLOCALBIN="${VIRTUAL_ENV}/usr/local/bin"
export _USRSBIN="${VIRTUAL_ENV}/usr/sbin"
export _USRSHARE="${VIRTUAL_ENV}/usr/share"
export _USRSRC="${VIRTUAL_ENV}/usr/src"
export _VAR="${VIRTUAL_ENV}/var"
export _VARCACHE="${VIRTUAL_ENV}/var/cache"
export _VARLIB="${VIRTUAL_ENV}/var/lib"
export _VARLOCK="${VIRTUAL_ENV}/var/lock"
export _LOG="${VIRTUAL_ENV}/var/log"
export _VARMAIL="${VIRTUAL_ENV}/var/mail"
export _VAROPT="${VIRTUAL_ENV}/var/opt"
export _VARRUN="${VIRTUAL_ENV}/var/run"
export _VARSPOOL="${VIRTUAL_ENV}/var/spool"
export _VARTMP="${VIRTUAL_ENV}/var/tmp"
export _WWW="${VIRTUAL_ENV}/var/www"
export _USRLOG="${VIRTUAL_ENV}/-usrlog.log"

Note

The --compress option is for documentation purposes only; without this option, paths are expanded in full.

JSON Configuration

venv.py --print-json dotfiles:

$ python ../scripts/venv.py --print-json dotfilesx dotfilesx/docs \
| python ../scripts/venv.py --compress dotfilesx dotfilesx/docs \
| sed "s,${HOME},~,g"
{
    "environ": {
        "HOME": "~",
        "__WRK": "${HOME}/-wrk",
        "__SRC": "${PROJECT_HOME}/-src",
        "__DOTFILES": "${HOME}/-dotfiles",
        "PROJECT_HOME": "${HOME}/-wrk",
        "WORKON_HOME__py27": "${PROJECT_HOME}/-ve27",
        "WORKON_HOME__py34": "${PROJECT_HOME}/-ve34",
        "WORKON_HOME_DEFAULT": "WORKON_HOME__py27",
        "CONDA_ROOT__py27": "${PROJECT_HOME}/-conda27",
        "CONDA_ENVS__py27": "${PROJECT_HOME}/-ce27",
        "CONDA_ROOT__py34": "${PROJECT_HOME}/-conda34",
        "CONDA_ENVS__py34": "${PROJECT_HOME}/-ce34",
        "CONDA_ROOT": "${PROJECT_HOME}/-conda27",
        "CONDA_ENVS_PATH": "${PROJECT_HOME}/-ce27",
        "WORKON_HOME": "${PROJECT_HOME}/-ve27",
        "VENVSTR": "${VENVSTR}",
        "VENVSTRAPP": "${VENVSTRAPP}",
        "VIRTUAL_ENV_NAME": "${VENVSTR}",
        "VIRTUAL_ENV": "${WORKON_HOME}/${VENVSTR}",
        "VENVPREFIX": "${WORKON_HOME}/${VENVSTR}",
        "_APP": "${VENVSTRAPP}",
        "_ETC": "${VIRTUAL_ENV}/etc",
        "_SRC": "${VIRTUAL_ENV}/src",
        "_WRD": "${_SRC}/${VENVSTRAPP}",
        "_BIN": "${VIRTUAL_ENV}/bin",
        "_ETCOPT": "${_ETC}/opt",
        "_HOME": "${VIRTUAL_ENV}/home",
        "_LIB": "${VIRTUAL_ENV}/lib",
        "_PYLIB": "${VIRTUAL_ENV}/lib/python2.7",
        "_PYSITE": "${VIRTUAL_ENV}/lib/python2.7/site-packages",
        "_MNT": "${VIRTUAL_ENV}/mnt",
        "_MEDIA": "${VIRTUAL_ENV}/media",
        "_OPT": "${VIRTUAL_ENV}/opt",
        "_ROOT": "${VIRTUAL_ENV}/root",
        "_SBIN": "${VIRTUAL_ENV}/sbin",
        "_SRV": "${VIRTUAL_ENV}/srv",
        "_TMP": "${VIRTUAL_ENV}/tmp",
        "_USR": "${VIRTUAL_ENV}/usr",
        "_USRBIN": "${VIRTUAL_ENV}/usr/bin",
        "_USRINCLUDE": "${VIRTUAL_ENV}/usr/include",
        "_USRLIB": "${VIRTUAL_ENV}/usr/lib",
        "_USRLOCAL": "${VIRTUAL_ENV}/usr/local",
        "_USRLOCALBIN": "${VIRTUAL_ENV}/usr/local/bin",
        "_USRSBIN": "${VIRTUAL_ENV}/usr/sbin",
        "_USRSHARE": "${VIRTUAL_ENV}/usr/share",
        "_USRSRC": "${VIRTUAL_ENV}/usr/src",
        "_VAR": "${VIRTUAL_ENV}/var",
        "_VARCACHE": "${VIRTUAL_ENV}/var/cache",
        "_VARLIB": "${VIRTUAL_ENV}/var/lib",
        "_VARLOCK": "${VIRTUAL_ENV}/var/lock",
        "_LOG": "${VIRTUAL_ENV}/var/log",
        "_VARMAIL": "${VIRTUAL_ENV}/var/mail",
        "_VAROPT": "${VIRTUAL_ENV}/var/opt",
        "_VARRUN": "${VIRTUAL_ENV}/var/run",
        "_VARSPOOL": "${VIRTUAL_ENV}/var/spool",
        "_VARTMP": "${VIRTUAL_ENV}/var/tmp",
        "_WWW": "${VIRTUAL_ENV}/var/www",
        "_USRLOG": "${VIRTUAL_ENV}/-usrlog.log"
    },
    "aliases": {
        "cdhome": "cdhome () {\n    # cdhome            -- cd $HOME /$@\n    [ -z \"$HOME\" ] && echo \"HOME is not set\" && return 1\n    cd \"$HOME\"${@:+\"/${@}\"}\n}\n_cd_HOME_complete () {\n    local cur=\"$2\";\n    COMPREPLY=($(cdhome && compgen -d -- \"${cur}\" ))\n}\ncdh () {\n    # cdh               -- cd $HOME\n    cdhome $@\n}\ncomplete -o default -o nospace -F _cd_HOME_complete cdhome\ncomplete -o default -o nospace -F _cd_HOME_complete cdh\n",
        "cdwrk": "cdwrk () {\n    # cdwrk             -- cd $__WRK /$@\n    [ -z \"$__WRK\" ] && echo \"__WRK is not set\" && return 1\n    cd \"$__WRK\"${@:+\"/${@}\"}\n}\n_cd___WRK_complete () {\n    local cur=\"$2\";\n    COMPREPLY=($(cdwrk && compgen -d -- \"${cur}\" ))\n}\ncomplete -o default -o nospace -F _cd___WRK_complete cdwrk\n",
        "cdddotfiles": "cddotfiles () {\n    # cddotfiles        -- cd $__DOTFILES /$@\n    [ -z \"$__DOTFILES\" ] && echo \"__DOTFILES is not set\" && return 1\n    cd \"$__DOTFILES\"${@:+\"/${@}\"}\n}\n_cd___DOTFILES_complete () {\n    local cur=\"$2\";\n    COMPREPLY=($(cddotfiles && compgen -d -- \"${cur}\" ))\n}\ncdd () {\n    # cdd               -- cd $__DOTFILES\n    cddotfiles $@\n}\ncomplete -o default -o nospace -F _cd___DOTFILES_complete cddotfiles\ncomplete -o default -o nospace -F _cd___DOTFILES_complete cdd\n",
        "cdprojecthome": "cdprojecthome () {\n    # cdprojecthome     -- cd $PROJECT_HOME /$@\n    [ -z \"$PROJECT_HOME\" ] && echo \"PROJECT_HOME is not set\" && return 1\n    cd \"$PROJECT_HOME\"${@:+\"/${@}\"}\n}\n_cd_PROJECT_HOME_complete () {\n    local cur=\"$2\";\n    COMPREPLY=($(cdprojecthome && compgen -d -- \"${cur}\" ))\n}\ncdp () {\n    # cdp               -- cd $PROJECT_HOME\n    cdprojecthome $@\n}\ncdph () {\n    # cdph              -- cd $PROJECT_HOME\n    cdprojecthome $@\n}\ncomplete -o default -o nospace -F _cd_PROJECT_HOME_complete cdprojecthome\ncomplete -o default -o nospace -F _cd_PROJECT_HOME_complete cdp\ncomplete -o default -o nospace -F _cd_PROJECT_HOME_complete cdph\n",
        "cdworkonhome": "cdworkonhome () {\n    # cdworkonhome      -- cd $WORKON_HOME /$@\n    [ -z \"$WORKON_HOME\" ] && echo \"WORKON_HOME is not set\" && return 1\n    cd \"$WORKON_HOME\"${@:+\"/${@}\"}\n}\n_cd_WORKON_HOME_complete () {\n    local cur=\"$2\";\n    COMPREPLY=($(cdworkonhome && compgen -d -- \"${cur}\" ))\n}\ncdwh () {\n    # cdwh              -- cd $WORKON_HOME\n    cdworkonhome $@\n}\ncdve () {\n    # cdve              -- cd $WORKON_HOME\n    cdworkonhome $@\n}\ncomplete -o default -o nospace -F _cd_WORKON_HOME_complete cdworkonhome\ncomplete -o default -o nospace -F _cd_WORKON_HOME_complete cdwh\ncomplete -o default -o nospace -F _cd_WORKON_HOME_complete cdve\n",
        "cdcondahome": "cdcondaenvspath () {\n    # cdcondaenvspath   -- cd $CONDA_ENVS_PATH /$@\n    [ -z \"$CONDA_ENVS_PATH\" ] && echo \"CONDA_ENVS_PATH is not set\" && return 1\n    cd \"$CONDA_ENVS_PATH\"${@:+\"/${@}\"}\n}\n_cd_CONDA_ENVS_PATH_complete () {\n    local cur=\"$2\";\n    COMPREPLY=($(cdcondaenvspath && compgen -d -- \"${cur}\" ))\n}\ncda () {\n    # cda               -- cd $CONDA_ENVS_PATH\n    cdcondaenvspath $@\n}\ncdce () {\n    # cdce              -- cd $CONDA_ENVS_PATH\n    cdcondaenvspath $@\n}\ncomplete -o default -o nospace -F _cd_CONDA_ENVS_PATH_complete cdcondaenvspath\ncomplete -o default -o nospace -F _cd_CONDA_ENVS_PATH_complete cda\ncomplete -o default -o nospace -F _cd_CONDA_ENVS_PATH_complete cdce\n",
        "cdvirtualenv": "cdvirtualenv () {\n    # cdvirtualenv      -- cd $VIRTUAL_ENV /$@\n    [ -z \"$VIRTUAL_ENV\" ] && echo \"VIRTUAL_ENV is not set\" && return 1\n    cd \"$VIRTUAL_ENV\"${@:+\"/${@}\"}\n}\n_cd_VIRTUAL_ENV_complete () {\n    local cur=\"$2\";\n    COMPREPLY=($(cdvirtualenv && compgen -d -- \"${cur}\" ))\n}\ncdv () {\n    # cdv               -- cd $VIRTUAL_ENV\n    cdvirtualenv $@\n}\ncomplete -o default -o nospace -F _cd_VIRTUAL_ENV_complete cdvirtualenv\ncomplete -o default -o nospace -F _cd_VIRTUAL_ENV_complete cdv\n",
        "cdsrc": "cdsrc () {\n    # cdsrc             -- cd $_SRC /$@\n    [ -z \"$_SRC\" ] && echo \"_SRC is not set\" && return 1\n    cd \"$_SRC\"${@:+\"/${@}\"}\n}\n_cd__SRC_complete () {\n    local cur=\"$2\";\n    COMPREPLY=($(cdsrc && compgen -d -- \"${cur}\" ))\n}\ncds () {\n    # cds               -- cd $_SRC\n    cdsrc $@\n}\ncomplete -o default -o nospace -F _cd__SRC_complete cdsrc\ncomplete -o default -o nospace -F _cd__SRC_complete cds\n",
        "cdwrd": "cdwrd () {\n    # cdwrd             -- cd $_WRD /$@\n    [ -z \"$_WRD\" ] && echo \"_WRD is not set\" && return 1\n    cd \"$_WRD\"${@:+\"/${@}\"}\n}\n_cd__WRD_complete () {\n    local cur=\"$2\";\n    COMPREPLY=($(cdwrd && compgen -d -- \"${cur}\" ))\n}\ncdw () {\n    # cdw               -- cd $_WRD\n    cdwrd $@\n}\ncomplete -o default -o nospace -F _cd__WRD_complete cdwrd\ncomplete -o default -o nospace -F _cd__WRD_complete cdw\n",
        "cdbin": "cdbin () {\n    # cdbin             -- cd $_BIN /$@\n    [ -z \"$_BIN\" ] && echo \"_BIN is not set\" && return 1\n    cd \"$_BIN\"${@:+\"/${@}\"}\n}\n_cd__BIN_complete () {\n    local cur=\"$2\";\n    COMPREPLY=($(cdbin && compgen -d -- \"${cur}\" ))\n}\ncdb () {\n    # cdb               -- cd $_BIN\n    cdbin $@\n}\ncomplete -o default -o nospace -F _cd__BIN_complete cdbin\ncomplete -o default -o nospace -F _cd__BIN_complete cdb\n",
        "cdetc": "cdetc () {\n    # cdetc             -- cd $_ETC /$@\n    [ -z \"$_ETC\" ] && echo \"_ETC is not set\" && return 1\n    cd \"$_ETC\"${@:+\"/${@}\"}\n}\n_cd__ETC_complete () {\n    local cur=\"$2\";\n    COMPREPLY=($(cdetc && compgen -d -- \"${cur}\" ))\n}\ncde () {\n    # cde               -- cd $_ETC\n    cdetc $@\n}\ncomplete -o default -o nospace -F _cd__ETC_complete cdetc\ncomplete -o default -o nospace -F _cd__ETC_complete cde\n",
        "cdlib": "cdlib () {\n    # cdlib             -- cd $_LIB /$@\n    [ -z \"$_LIB\" ] && echo \"_LIB is not set\" && return 1\n    cd \"$_LIB\"${@:+\"/${@}\"}\n}\n_cd__LIB_complete () {\n    local cur=\"$2\";\n    COMPREPLY=($(cdlib && compgen -d -- \"${cur}\" ))\n}\ncdl () {\n    # cdl               -- cd $_LIB\n    cdlib $@\n}\ncomplete -o default -o nospace -F _cd__LIB_complete cdlib\ncomplete -o default -o nospace -F _cd__LIB_complete cdl\n",
        "cdlog": "cdlog () {\n    # cdlog             -- cd $_LOG /$@\n    [ -z \"$_LOG\" ] && echo \"_LOG is not set\" && return 1\n    cd \"$_LOG\"${@:+\"/${@}\"}\n}\n_cd__LOG_complete () {\n    local cur=\"$2\";\n    COMPREPLY=($(cdlog && compgen -d -- \"${cur}\" ))\n}\ncomplete -o default -o nospace -F _cd__LOG_complete cdlog\n",
        "cdpylib": "cdpylib () {\n    # cdpylib           -- cd $_PYLIB /$@\n    [ -z \"$_PYLIB\" ] && echo \"_PYLIB is not set\" && return 1\n    cd \"$_PYLIB\"${@:+\"/${@}\"}\n}\n_cd__PYLIB_complete () {\n    local cur=\"$2\";\n    COMPREPLY=($(cdpylib && compgen -d -- \"${cur}\" ))\n}\ncomplete -o default -o nospace -F _cd__PYLIB_complete cdpylib\n",
        "cdpysite": "cdpysite () {\n    # cdpysite          -- cd $_PYSITE /$@\n    [ -z \"$_PYSITE\" ] && echo \"_PYSITE is not set\" && return 1\n    cd \"$_PYSITE\"${@:+\"/${@}\"}\n}\n_cd__PYSITE_complete () {\n    local cur=\"$2\";\n    COMPREPLY=($(cdpysite && compgen -d -- \"${cur}\" ))\n}\ncdsitepackages () {\n    # cdsitepackages    -- cd $_PYSITE\n    cdpysite $@\n}\ncomplete -o default -o nospace -F _cd__PYSITE_complete cdpysite\ncomplete -o default -o nospace -F _cd__PYSITE_complete cdsitepackages\n",
        "cdvar": "cdvar () {\n    # cdvar             -- cd $_VAR /$@\n    [ -z \"$_VAR\" ] && echo \"_VAR is not set\" && return 1\n    cd \"$_VAR\"${@:+\"/${@}\"}\n}\n_cd__VAR_complete () {\n    local cur=\"$2\";\n    COMPREPLY=($(cdvar && compgen -d -- \"${cur}\" ))\n}\ncomplete -o default -o nospace -F _cd__VAR_complete cdvar\n",
        "cdwww": "cdwww () {\n    # cdwww             -- cd $_WWW /$@\n    [ -z \"$_WWW\" ] && echo \"_WWW is not set\" && return 1\n    cd \"$_WWW\"${@:+\"/${@}\"}\n}\n_cd__WWW_complete () {\n    local cur=\"$2\";\n    COMPREPLY=($(cdwww && compgen -d -- \"${cur}\" ))\n}\ncdww () {\n    # cdww              -- cd $_WWW\n    cdwww $@\n}\ncomplete -o default -o nospace -F _cd__WWW_complete cdwww\ncomplete -o default -o nospace -F _cd__WWW_complete cdww\n",
        "cdls": "set | grep \"^cd.*()\" | cut -f1 -d\" \" #%l",
        "cdhelp": "cat ${__DOTFILES}/''scripts/venv_cdaliases.sh | pyline.py -r '^\\s*#+\\s+.*' 'rgx and l'"
    }
}

Other scripts with venv-style paths

To define a script environment just like venv:

#!/bin/sh

__WRK="${HOME}/-wrk"                          # cdwrk
__DOTFILES="${HOME}/-dotfiles"                # cdd cddotfiles
PROJECT_HOME="${__WRK}"                       # cdph cdprojecthome
WORKON_HOME="${__WRK}/-ve27"                  # cdwh cdworkonhome

CONDA_ROOT="${__WRK}/-conda27"                # $CONDA_ROOT__py27
CONDA_ENVS_PATH="${__WRK}/-ce27"              # cdce cda cdcondaenvspath

VIRTUAL_ENV_NAME="dotfiles"                   # 'dotfiles'
_APP=$VIRTUAL_ENV_NAME                        # 'dotfiles[/p/a/t/h]'
VIRTUAL_ENV="$WORKON_HOME/$VIRTUAL_ENV_NAME"  # cdv cdvirtualenv
_SRC="${VIRTUAL_ENV}/src"                     # cds cdsrc
_BIN="${VIRTUAL_ENV}/bin"                     # cde cdbin
_ETC="${VIRTUAL_ENV}/etc"                     # cde cdetc
_LOG="${VIRTUAL_ENV}/var/log"                 # cdl cdlog
# ... see: venv.py --print-vars / ...
_WRD="${_SRC}/{_APP}"                         # cdw cdwrd

(set -x; test "$_WRD" == "${HOME}/-wrk/-ve27/dotfiles/src/dotfiles"; \
    || echo "Exception: _WRD = '${_WRD}';" )