Instantly share code, notes, and snippets.

Embed
What would you like to do?
virtualenv-auto-activate
#!/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" != "$(pwd -P)/.venv" ]; 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
fi
}
export PROMPT_COMMAND=_virtualenv_auto_activate
@cwells

This comment has been minimized.

cwells commented Apr 20, 2013

You should probably verify that the .venv file is owned by the current user and has safe permissions (e.g. 0600). I'd also disallow root from doing this. Otherwise, this is known as a big security hole (cd into a dir and execute random hidden source file).

You could also restrict execution to subdirectories of the user's home dir.

But still, a nice snippet, thanks.

@cwells

This comment has been minimized.

cwells commented Apr 20, 2013

Adding this would probably be sufficient:

# only execute if .venv is owned by user and has safe permissions
if [ `stat -c "%a %u" .venv` = `echo 0600\`id -u\`` ]; then
@hfeeki

This comment has been minimized.

hfeeki commented Jun 2, 2013

if [ `stat -c "%u" .venv` = `id -u` ]; then

above code can works in my mac

@ceospfx

This comment has been minimized.

ceospfx commented Jan 28, 2014

This actually works pretty well. The only issue I've run into is that you can not 'deactivate' the virtual environment unless you go outside of the directory. I wonder if there is a way to deactivate it at any level?

@brianpkennedy

This comment has been minimized.

brianpkennedy commented Feb 11, 2014

I was looking for something just like this, however to play nice with other PROMPT_COMMANDs it should end with

PROMPT_COMMAND="_virtualenv_auto_activate; $PROMPT_COMMAND"

I also had to do a fix for .venv symlinks, as I prefer to have one directory.

Forked here : https://gist.github.com/brianpkennedy/8943902

@Sashkow

This comment has been minimized.

Sashkow commented Feb 12, 2015

where should I put this script?

@felippemr

This comment has been minimized.

@markph0204

This comment has been minimized.

markph0204 commented Dec 26, 2017

I've head varying results with autoenv, try direnv for reliable results.

@amalsalim

This comment has been minimized.

amalsalim commented Mar 5, 2018

i didn't know about PROMPT_COMMAND. thank you very much!

@fmoor

This comment has been minimized.

fmoor commented Jul 1, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment