Venv¶
Venv makes working with Virtualenvwrapper, Bash, and IPython very easy.
There are three parts to “venv”:
10-bashrc.venv.sh (docs) configures variables like $VIRTUAL_ENV_NAME, $_SRC, and $_WRD; and functions like we() and e() for Bash (and ZSH).
dotfiles.venv.ipython_config.py (dotfiles.venv.ipython_config) provides a shell command (venv) called by we() for generating shell configuration for a Virtualenv and configures IPython.
dotfiles.venv.ipython_magics.py (dotfiles.venv.ipython_magics) configures the same cd commands and ds command defined in 10-bashrc.venv.sh and ipython_config.py for IPython.
Quickstart¶
# print shell configuration for a (hypothetical) dotfiles virtualenv
venv dotfiles --bash
# print shell configuration for the current ${VIRTUAL_ENV} [and ${_WRD}]
venv -E --bash
# run a command within a virtualenv
venv dotfiles -x bash
# workon a virtualenvwrapper virtualenv (we) (source <(venv -E --bash))
we dotfiles
# workon ${WORKON_HOME}/dotfiles/src/otherproject (echo $_APP $_WRD)
we dotfiles otherproject
Usage¶
Shell Command¶
$ python ../src/dotfiles/venv/ipython_config.py --help
Usage: ipython_config.py [-b|--print-bash] [-t] [-e] [-E<virtualenv>] [appname]
dotfiles.venv.ipython_config.py
Options:
-h, --help show this help message and exit
-E, --from-shell-environ
-p, --print, --print-json, --json
Print venv configuration as JSON
-b, --bash, --print-bash, --zsh, --print-zsh
Print venv configuration for Bash, ZSH
-x RUN_COMMAND, --cmd=RUN_COMMAND, --command=RUN_COMMAND
Run a command in a venv-configured shell
-t, --terminals, --open-terminals
Open terminals within the venv [gnome-terminal]
-e, --editors, --open-editors
Open an editor with venv._project_files
[$PROJECT_FILES]
--platform=PLATFORM Platform to generate configuration for
-v, --verbose
-q, --quiet
-T, --test
Copyright 2014 Wes Turner. New BSD License
Python API¶
A dotfiles.venv.ipython_config.Venv object builds a dotfiles.venv.ipython_config.Env OrderedDict (.env) with $VIRTUAL_ENV-relative paths and environment variables in a common filesystem hierarchy and an OrderedDict of command aliases (.aliases), which can be serialized to a bash script (venv --bash), JSON (venv --print), and IPython configuration.
from dotfiles.venv.ipython_config import Venv
venv = Venv(from_environ=True)
venv.print()
venv.bash_env()
venv.configure_sys()
venv.configure_ipython()
assert venv.virtualenv == venv.env['VIRTUAL_ENV']
assert venv.appname == venv.env['_APP']
print(venv.env['_WRD']) # working directory
print(venv.aliases['e']) # edit with --servername $_APP
Example Venv Configuration¶
Shell Configuration¶
venv dotfiles --bash:
$ python ../src/dotfiles/venv/ipython_config.py dotfiles --bash \
| sed "s,${HOME},~,g"
export VIRTUAL_ENV='~/wrk/.ve/dotfiles'
export VIRTUAL_ENV_NAME='dotfiles'
export _BIN='~/wrk/.ve/dotfiles/bin'
export _ETC='~/wrk/.ve/dotfiles/etc'
export _ETCOPT='~/wrk/.ve/dotfiles/etc/opt'
export _HOME='~/wrk/.ve/dotfiles/home'
export _ROOT='~/wrk/.ve/dotfiles/root'
export _LIB='~/wrk/.ve/dotfiles/lib'
export _PYLIB='~/wrk/.ve/dotfiles/lib/python2.7'
export _PYSITE='~/wrk/.ve/dotfiles/lib/python2.7/site-packages'
export _MNT='~/wrk/.ve/dotfiles/mnt'
export _MEDIA='~/wrk/.ve/dotfiles/media'
export _OPT='~/wrk/.ve/dotfiles/opt'
export _SBIN='~/wrk/.ve/dotfiles/sbin'
export _SRC='~/wrk/.ve/dotfiles/src'
export _SRV='~/wrk/.ve/dotfiles/srv'
export _TMP='~/wrk/.ve/dotfiles/tmp'
export _USR='~/wrk/.ve/dotfiles/usr'
export _USRBIN='~/wrk/.ve/dotfiles/usr/bin'
export _USRINCLUDE='~/wrk/.ve/dotfiles/usr/include'
export _USRLIB='~/wrk/.ve/dotfiles/usr/lib'
export _USRLOCAL='~/wrk/.ve/dotfiles/usr/local'
export _USRSBIN='~/wrk/.ve/dotfiles/usr/sbin'
export _USRSHARE='~/wrk/.ve/dotfiles/usr/share'
export _USRSRC='~/wrk/.ve/dotfiles/usr/src'
export _VAR='~/wrk/.ve/dotfiles/var'
export _VARCACHE='~/wrk/.ve/dotfiles/var/cache'
export _VARLIB='~/wrk/.ve/dotfiles/var/lib'
export _VARLOCK='~/wrk/.ve/dotfiles/var/lock'
export _LOG='~/wrk/.ve/dotfiles/var/log'
export _VARMAIL='~/wrk/.ve/dotfiles/var/mail'
export _VAROPT='~/wrk/.ve/dotfiles/var/opt'
export _VARRUN='~/wrk/.ve/dotfiles/var/run'
export _VARSPOOL='~/wrk/.ve/dotfiles/var/spool'
export _VARTMP='~/wrk/.ve/dotfiles/var/tmp'
export _WWW='~/wrk/.ve/dotfiles/var/www'
export _USRLOG='~/wrk/.ve/dotfiles/.usrlog'
export HISTFILE='~/wrk/.ve/dotfiles/.bash_history'
export HISTSIZE=1000000
export HISTFILESIZE=1000000
export PAGER='/usr/bin/less -R'
export _APP='dotfiles'
export _WRD='~/wrk/.ve/dotfiles/src/dotfiles'
export VIMBIN='/usr/bin/vim'
export GVIMBIN=None
export MVIMBIN=None
export GUIVIMBIN=None
export VIMCONF='--servername dotfiles'
export _EDIT_='/usr/bin/vim -f'
export EDITOR_='/usr/bin/vim -f'
export _IPSESSKEY='~/wrk/.ve/dotfiles/src/.sessionkey'
export _NOTEBOOKS='~/wrk/.ve/dotfiles/src/notebooks'
export _IPQTLOG='~/wrk/.ve/dotfiles/.ipqt.log'
export _WRD_SETUPY='~/wrk/.ve/dotfiles/src/dotfiles/setup.py'
export _TEST_='(cd "${_WRD}" && python "${_WRD_SETUPY}" test)'
export _CFG='~/wrk/.ve/dotfiles/etc/development.ini'
export _EDITCFG_='/usr/bin/vim -f ~/wrk/.ve/dotfiles/etc/development.ini'
export _SHELL_='(cd "${_WRD}" && "${_BIN}"/pshell "${_CFG}")'
export _SERVE_='(cd "${_WRD}" && "${_BIN}"/pserve --app-name=main --reload --monitor-restart "${_CFG}")'
export PROJECT_FILES='README.rst CHANGES.rst TODO.rst Makefile setup.py requirements.txt .hgignore .gitignore .hg/hgrc dotfiles docs'
export _SVCFG='~/wrk/.ve/dotfiles/etc/supervisord.conf'
export _SVCFG_=' -c "~/wrk/.ve/dotfiles/etc/supervisord.conf"'
eval 'cdb () {
cd "${_BIN}"/$@
}';
eval 'cde () {
cd "${_ETC}"/$@
}';
eval 'cdh () {
cd "${HOME}"/$@
}';
eval 'cdl () {
cd "${_LIB}"/$@
}';
eval 'cdlog () {
cd "${_LOG}"/$@
}';
eval 'cdp () {
cd "${PROJECT_HOME}"/$@
}';
eval 'cdph () {
cd "${PROJECT_HOME}"/$@
}';
eval 'cdpylib () {
cd "${_PYLIB}"/$@
}';
eval 'cdpysite () {
cd "${_PYSITE}"/$@
}';
eval 'cds () {
cd "${_SRC}"/$@
}';
eval 'cdv () {
cd "${VIRTUAL_ENV}"/$@
}';
eval 'cdve () {
cd "${VIRTUAL_ENV}"/$@
}';
eval 'cdvar () {
cd "${_VAR}"/$@
}';
eval 'cdwh () {
cd "${WORKON_HOME}"/$@
}';
eval 'cdwrk () {
cd "${WORKON_HOME}"/$@
}';
eval 'cdw () {
cd "${_WRD}"/$@
}';
eval 'cd- () {
cd "${_WRD}"/$@
}';
eval 'cdww () {
cd "${_WWW}"/$@
}';
eval 'cdwww () {
cd "${_WWW}"/$@
}';
eval 'cdhelp () {
set | grep "^cd.*()" | cut -f1 -d" " #$@
}';
eval 'edit- () {
${_EDIT_} $@
}';
alias gvim-='${VIMBIN} -f'
eval 'ipskey () {
(python -c "import os; print os.urandom(128).encode(\"base64\")" > "${_IPSESSKEY}" ) && chmod 0600 "${_IPSESSKEY}"; # $@
}';
eval 'ipnb () {
ipython notebook --secure --Session.keyfile="${_IPSESSKEY}" --notebook-dir="${_NOTEBOOKS}" --deep-reload $@
}';
eval 'ipqt () {
ipython qtconsole --secure --Session.keyfile="${_IPSESSKEY}" --logappend="${_IPQTLOG}" --deep-reload --pprint --colors=linux --ConsoleWidget.font_family="Monaco" --ConsoleWidget.font_size=11 $@
}';
eval 'grinv () {
grin --follow $@ "${VIRTUAL_ENV}"
}';
eval 'grindv () {
grind --follow $@ --dirs "${VIRTUAL_ENV}"
}';
eval 'grins () {
grin --follow $@ "${_SRC}"
}';
eval 'grinds () {
grind --follow $@ --dirs "${_SRC}"
}';
alias test-='(cd "${_WRD}" && python "${_WRD_SETUPY}" test)'
alias testr-='reset && (cd "${_WRD}" && python "${_WRD_SETUPY}" test)'
alias nose-='(cd "${_WRD}" && nosetests)'
eval 'grinw () {
grin --follow $@ "${_WRD}"
}';
eval 'grin- () {
grin --follow $@ "${_WRD}"
}';
eval 'grindw () {
grind --follow $@ --dirs "${_WRD}"
}';
eval 'grind- () {
grind --follow $@ --dirs "${_WRD}"
}';
alias hgv-='hg view -R "${_WRD}"'
alias hgl-='hg -R "${_WRD}" log'
eval 'editcfg () {
"${_EDITCFG_}" $@
}';
alias serve-='(cd "${_WRD}" && "${_BIN}"/pserve --app-name=main --reload --monitor-restart "${_CFG}")'
alias shell-='(cd "${_WRD}" && "${_BIN}"/pshell "${_CFG}")'
eval 'e () {
${_EDIT_} $@
}';
eval 'editp () {
$GUIVIMBIN $VIMCONF $PROJECT_FILES $@
}';
eval 'makewrd () {
(cd "${_WRD}" && make $@)
}';
eval 'make- () {
(cd "${_WRD}" && make $@)
}';
eval 'mw () {
(cd "${_WRD}" && make $@)
}';
alias ssv='supervisord -c "${_SVCFG}"'
alias sv='supervisorctl -c "${_SVCFG}"'
alias svt='sv tail -f'
alias svd='supervisorctl -c "${_SVCFG}" restart dev && supervisorctl -c "${_SVCFG}" tail -f dev'
JSON Configuration¶
venv dotfiles --print:
$ python ../src/dotfiles/venv/ipython_config.py dotfiles --print \
| sed "s,${HOME},~,g"
{
"env": {
"VIRTUAL_ENV": "~/wrk/.ve/dotfiles",
"VIRTUAL_ENV_NAME": "dotfiles",
"_BIN": "~/wrk/.ve/dotfiles/bin",
"_ETC": "~/wrk/.ve/dotfiles/etc",
"_ETCOPT": "~/wrk/.ve/dotfiles/etc/opt",
"_HOME": "~/wrk/.ve/dotfiles/home",
"_ROOT": "~/wrk/.ve/dotfiles/root",
"_LIB": "~/wrk/.ve/dotfiles/lib",
"_PYLIB": "~/wrk/.ve/dotfiles/lib/python2.7",
"_PYSITE": "~/wrk/.ve/dotfiles/lib/python2.7/site-packages",
"_MNT": "~/wrk/.ve/dotfiles/mnt",
"_MEDIA": "~/wrk/.ve/dotfiles/media",
"_OPT": "~/wrk/.ve/dotfiles/opt",
"_SBIN": "~/wrk/.ve/dotfiles/sbin",
"_SRC": "~/wrk/.ve/dotfiles/src",
"_SRV": "~/wrk/.ve/dotfiles/srv",
"_TMP": "~/wrk/.ve/dotfiles/tmp",
"_USR": "~/wrk/.ve/dotfiles/usr",
"_USRBIN": "~/wrk/.ve/dotfiles/usr/bin",
"_USRINCLUDE": "~/wrk/.ve/dotfiles/usr/include",
"_USRLIB": "~/wrk/.ve/dotfiles/usr/lib",
"_USRLOCAL": "~/wrk/.ve/dotfiles/usr/local",
"_USRSBIN": "~/wrk/.ve/dotfiles/usr/sbin",
"_USRSHARE": "~/wrk/.ve/dotfiles/usr/share",
"_USRSRC": "~/wrk/.ve/dotfiles/usr/src",
"_VAR": "~/wrk/.ve/dotfiles/var",
"_VARCACHE": "~/wrk/.ve/dotfiles/var/cache",
"_VARLIB": "~/wrk/.ve/dotfiles/var/lib",
"_VARLOCK": "~/wrk/.ve/dotfiles/var/lock",
"_LOG": "~/wrk/.ve/dotfiles/var/log",
"_VARMAIL": "~/wrk/.ve/dotfiles/var/mail",
"_VAROPT": "~/wrk/.ve/dotfiles/var/opt",
"_VARRUN": "~/wrk/.ve/dotfiles/var/run",
"_VARSPOOL": "~/wrk/.ve/dotfiles/var/spool",
"_VARTMP": "~/wrk/.ve/dotfiles/var/tmp",
"_WWW": "~/wrk/.ve/dotfiles/var/www",
"_USRLOG": "~/wrk/.ve/dotfiles/.usrlog",
"HISTFILE": "~/wrk/.ve/dotfiles/.bash_history",
"HISTSIZE": 1000000,
"HISTFILESIZE": 1000000,
"PAGER": "/usr/bin/less -R",
"_APP": "dotfiles",
"_WRD": "~/wrk/.ve/dotfiles/src/dotfiles",
"VIMBIN": "/usr/bin/vim",
"GVIMBIN": null,
"MVIMBIN": null,
"GUIVIMBIN": null,
"VIMCONF": "--servername dotfiles",
"_EDIT_": "/usr/bin/vim -f",
"EDITOR_": "/usr/bin/vim -f",
"_IPSESSKEY": "~/wrk/.ve/dotfiles/src/.sessionkey",
"_NOTEBOOKS": "~/wrk/.ve/dotfiles/src/notebooks",
"_IPQTLOG": "~/wrk/.ve/dotfiles/.ipqt.log",
"_WRD_SETUPY": "~/wrk/.ve/dotfiles/src/dotfiles/setup.py",
"_TEST_": "(cd \"${_WRD}\" && python \"${_WRD_SETUPY}\" test)",
"_CFG": "~/wrk/.ve/dotfiles/etc/development.ini",
"_EDITCFG_": "/usr/bin/vim -f ~/wrk/.ve/dotfiles/etc/development.ini",
"_SHELL_": "(cd \"${_WRD}\" && \"${_BIN}\"/pshell \"${_CFG}\")",
"_SERVE_": "(cd \"${_WRD}\" && \"${_BIN}\"/pserve --app-name=main --reload --monitor-restart \"${_CFG}\")",
"PROJECT_FILES": "README.rst CHANGES.rst TODO.rst Makefile setup.py requirements.txt .hgignore .gitignore .hg/hgrc dotfiles docs",
"_SVCFG": "~/wrk/.ve/dotfiles/etc/supervisord.conf",
"_SVCFG_": " -c \"~/wrk/.ve/dotfiles/etc/supervisord.conf\""
},
"aliases": {
"cdb": "cd \"${_BIN}\"/%l",
"cde": "cd \"${_ETC}\"/%l",
"cdh": "cd \"${HOME}\"/%l",
"cdl": "cd \"${_LIB}\"/%l",
"cdlog": "cd \"${_LOG}\"/%l",
"cdp": "cd \"${PROJECT_HOME}\"/%l",
"cdph": "cd \"${PROJECT_HOME}\"/%l",
"cdpylib": "cd \"${_PYLIB}\"/%l",
"cdpysite": "cd \"${_PYSITE}\"/%l",
"cds": "cd \"${_SRC}\"/%l",
"cdv": "cd \"${VIRTUAL_ENV}\"/%l",
"cdve": "cd \"${VIRTUAL_ENV}\"/%l",
"cdvar": "cd \"${_VAR}\"/%l",
"cdwh": "cd \"${WORKON_HOME}\"/%l",
"cdwrk": "cd \"${WORKON_HOME}\"/%l",
"cdw": "cd \"${_WRD}\"/%l",
"cd-": "cd \"${_WRD}\"/%l",
"cdww": "cd \"${_WWW}\"/%l",
"cdwww": "cd \"${_WWW}\"/%l",
"cdhelp": "set | grep \"^cd.*()\" | cut -f1 -d\" \" #%l",
"edit-": "${_EDIT_} %l",
"gvim-": "/usr/bin/vim -f",
"ipskey": "(python -c \"import os; print os.urandom(128).encode(\\\"base64\\\")\" > \"${_IPSESSKEY}\" ) && chmod 0600 \"${_IPSESSKEY}\"; # %l",
"ipnb": "ipython notebook --secure --Session.keyfile=\"${_IPSESSKEY}\" --notebook-dir=\"${_NOTEBOOKS}\" --deep-reload %l",
"ipqt": "ipython qtconsole --secure --Session.keyfile=\"${_IPSESSKEY}\" --logappend=\"${_IPQTLOG}\" --deep-reload --pprint --colors=linux --ConsoleWidget.font_family=\"Monaco\" --ConsoleWidget.font_size=11 %l",
"grinv": "grin --follow %l \"${VIRTUAL_ENV}\"",
"grindv": "grind --follow %l --dirs \"${VIRTUAL_ENV}\"",
"grins": "grin --follow %l \"${_SRC}\"",
"grinds": "grind --follow %l --dirs \"${_SRC}\"",
"test-": "(cd \"${_WRD}\" && python \"${_WRD_SETUPY}\" test)",
"testr-": "reset && (cd \"${_WRD}\" && python \"${_WRD_SETUPY}\" test)",
"nose-": "(cd \"${_WRD}\" && nosetests)",
"grinw": "grin --follow %l \"${_WRD}\"",
"grin-": "grin --follow %l \"${_WRD}\"",
"grindw": "grind --follow %l --dirs \"${_WRD}\"",
"grind-": "grind --follow %l --dirs \"${_WRD}\"",
"hgv-": "hg view -R \"${_WRD}\"",
"hgl-": "hg -R \"${_WRD}\" log",
"editcfg": "\"${_EDITCFG_}\" %l",
"serve-": "(cd \"${_WRD}\" && \"${_BIN}\"/pserve --app-name=main --reload --monitor-restart \"${_CFG}\")",
"shell-": "(cd \"${_WRD}\" && \"${_BIN}\"/pshell \"${_CFG}\")",
"e": "${_EDIT_} %l",
"editp": "$GUIVIMBIN $VIMCONF $PROJECT_FILES %l",
"makewrd": "(cd \"${_WRD}\" && make %l)",
"make-": "(cd \"${_WRD}\" && make %l)",
"mw": "(cd \"${_WRD}\" && make %l)",
"ssv": "supervisord -c \"${_SVCFG}\"",
"sv": "supervisorctl -c \"${_SVCFG}\"",
"svt": "sv tail -f",
"svd": "supervisorctl -c \"${_SVCFG}\" restart dev && supervisorctl -c \"${_SVCFG}\" tail -f dev"
}
}