Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Automatic virtualenv activation and deactivation, virtualenv non lazy mode. #517

Closed
wants to merge 1 commit into from

2 participants

Keerthan Jaic Sorin Ionescu
Keerthan Jaic

chpwd hook for automatically activating and deactivating virtualenvs, and added option to load virtualenvwrapper normally(as opposed to lazy).

more info in README.md :)

Sorin Ionescu

Can automatically activating and deactivating virtualenvs interfere with currently running process or projects causing issues. Under what circumstances can this cause issues?

Keerthan Jaic
jck commented

The only way I can imagine it interfering is if the user manually activates a virtualenv, in those cases this extension does not function.

Sorin Ionescu

Does this affect presently running applications/servers?

Sorin Ionescu

I've been looking at random Python projects. Sometimes, .venv is a file; other times, it's a directory.

Keerthan Jaic

Activating virtualenvs only affect the current shell session, so I don't think it will interfere with running applications, servers.

Keerthan Jaic

We could add an option to customize the name of the file, to use something other than .venv

Sorin Ionescu

I would rather have a standard like .RUBY_VERSION.

Keerthan Jaic

I don't beleive there is any convention for python virtualenvs. However, oh my zsh uses .venv as the file name for this purpose.

Keerthan Jaic
jck commented

The example in the 'tips and tricks' section of virtualenvwrapper's docs uses a .venv file for this functionality.

http://virtualenvwrapper.readthedocs.org/en/latest/tips.html

Keerthan Jaic
jck commented

I'm deleting this pull request to pursue a more general alternative such as zsh-autoenv.

Keerthan Jaic jck closed this
Keerthan Jaic jck deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 74 additions and 2 deletions.
  1. +23 −0 modules/python/README.md
  2. +51 −2 modules/python/init.zsh
23 modules/python/README.md
View
@@ -61,6 +61,29 @@ system site-packages directory.
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--no-site-packages'
+### Settings
+
+#### Lazy loading
+
+By default, virtualenvwrapper is loaded in lazy mode. This behaviour can be disabled adding the following line to *~/.zpreztorc*:
+
+ zstyle ':prezto:module:python' lazy_venv 'no'
+
+#### Automatic virtualenv initialization
+
+To enable autoenv, add the following line to *~/.zpreztorc*:
+
+ zstyle ':prezto:module:python' autoenv 'yes'
+
+If a virtualenv is active, and was not activated by autoenv, then autoenv does nothing.
+Otherwise, autoenv activated or deactivates a virtual environment whenever a directory is changed; based on the following priority:
+
+ 1. Name specified in a file named *.venv* in the cwd.
+ 2. *.venv* file in git root. If folder is inside git repository.
+ 3. Name of git root folder.
+ 4. No virtualenv.
+
+
Aliases
-------
53 modules/python/init.zsh
View
@@ -31,17 +31,66 @@ if (( ! $+commands[python] && ! $+commands[pyenv] )); then
return 1
fi
+local venv_script=virtualenvwrapper
+if zstyle -T ':prezto:module:python' lazy_venv; then
+ venv_script+=_lazy
+fi
+
# Load virtualenvwrapper into the shell session.
-if (( $+commands[virtualenvwrapper_lazy.sh] )); then
+if (( $+commands[$venv_script.sh] )); then
# Set the directory where virtual environments are stored.
export WORKON_HOME="$HOME/.virtualenvs"
# Disable the virtualenv prompt.
VIRTUAL_ENV_DISABLE_PROMPT=1
- source "$commands[virtualenvwrapper_lazy.sh]"
+ source "$commands[$venv_script.sh]"
fi
+function autoenv {
+ #Don't run in shell scripts etc.
+ if [[ $ZSH_SUBSHELL -ne 0 ]]; then
+ return
+ fi
+
+ #Don't run if currently in virtual env not set by autoenv
+ if (( ($+VIRTUAL_ENV) && !($+AUTOENV) )); then
+ return
+ fi
+
+ local name=""
+ if [[ -f .venv ]]; then
+ #.venv file in current dir gets highest priority
+ name=$(<.venv)
+ elif is-true "$(git rev-parse --is-inside-work-tree 2> /dev/null)"; then
+ local gitroot="$(git rev-parse --show-toplevel 2> /dev/null)"
+ if [ -f "$gitroot/.venv" ]; then
+ #If there is a .venv file in gitroot
+ name=$(<$gitroot/.venv)
+ else
+ #Else use the name of the folder as venv name
+ name=$gitroot:t
+ fi
+ fi
+
+
+ local venv_name="$VIRTUAL_ENV:t"
+ if [[ $name != $venv_name ]]; then
+ if [[ -z $name ]]; then
+ deactivate && unset AUTOENV
+ return
+ fi
+
+ if [[ -d "$WORKON_HOME/$name" ]]; then
+ workon $name && export AUTOENV=1
+ return
+ fi
+ fi
+}
+
+if zstyle -T ':prezto:module:python' autoenv; then
+ add-zsh-hook chpwd autoenv
+fi
#
# Aliases
#
Something went wrong with that request. Please try again.