-
Notifications
You must be signed in to change notification settings - Fork 18
Python Virtualenv Post Activation Hooks
The virtualenv system has this mechanism called
post activation hooks. They're pretty cool.
So the idea is, every time you type workon FOO
, it goes and runs a bit of code from your home directory
(technically, it's sourcing a file into your environment, but you can mostly ignore the distinction until later.)
This can be used to do things like set up AWS keys, set different pagers or text editors for different projects,
or whatever you want.
The problem with this is that virtualenvwrapper only supports one post activation hook file, which by default lives
in ~/.virtualenvs/postactivate
. If you want to do different things for different projects, this file gets pretty
hairy pretty quickly.
So here I fixed it for you.
Replace ~/.virtualenvs/postactivate
with a new postactivate file, owned by you with permissions set to 0700.
Now, every time you type workon FOO
, virtualenvwrapper will look in two places for activation hooks for FOO: in
your environment directory, and in that environment's project directory (if you've set a .project file).
To give a concrete example, my environments are in ~/src/venvs and my projects are in ~/src/projects
. When I say
workon MY_GREAT_PROJECT
I want it to automatically change directory to the ~/src/projects/MY_GREAT_PROJECT
directory, so I create a file in ~/src/venvs/MY_GREAT_PROJECT
called .project, which contains only the line:
/home/USERNAME/src/projects/MY_GREAT_PROJECT
Then, I create a file in ~/src/projects/MY_GREAT_PROJECT
called postactivate which says:
export MY_GREAT_VARIABLE="Yay puppies"
Now, every time I type 'workon certs', it does two things: 1) it exports the variable MY_GREAT_VARIABLE
into my
environment, and 2) it changes directories to ~/src/projects/MY_GREAT_PROJECT
.
This is way convenient.