skip to navigation
skip to content

Not Logged In

virtualenv-activator 0.1.0

A better activate script for Python's virtualenv

Latest Version: 0.1.2

A better activate script for Python’s virtualenv


The default activate script had some problems that bugged me:

  • It doesn’t work with virtualenv --relocatable so you can’t move or rename your virtual environment directory.
  • I wanted an easy way to modify the shell environment (manually or automatically) inside a virtual environment and have it restored when I deactivate it.
  • Some other stuff I can’t remember.

My activate script automatically…

  • makes your virtual environment relocatable and keeps it that way even when you install new stuff (caveat: don’t move or rename your virtual environment while it’s activated).
  • sources an optional etc/ file where you can modify your shell environment every time your virtual environment is activated.
  • restores your shell environment upon deactivation to the state it was in before you activated.


Create a new virtual environment, install virtualenv-activator inside it, and then activate it using the etc/ script.

$ cd /path/to/my_code
$ virtualenv venv
$ venv/bin/pip install virtualenv-activator
$ . venv/etc/
(my_code) $

Deactivate as normal…

(my_code) $ deactivate

Well, it’s not as normal as it looks. There’s some magic happening under the hood. Try setting an environment variable in the etc/ file (this file doesn’t exist by default) in your virtual environment, but also set the same variable to a different value while you’re outside the virtual environment, and then watch what happens the variable as you activate and deactivate the environment.

$ echo 'export MY_VAR="inside venv"' > venv/etc/
$ export MY_VAR="outside venv"
$ echo $MY_VAR
outside venv
$ . venv/etc/
(my_code) $ echo $MY_VAR
inside venv
(my_code) $ export MY_VAR="manually set"
(my_code) $ echo $MY_VAR
manually set
(my_code) $ deactivate
$ echo $MY_VAR
outside venv
$ . venv/etc/
(my_code) $ echo $MY_VAR
inside venv

Basically, you can put pretty much anything bash related (variables, aliases, functions, etc.) in the etc/ script and it will only exist when your virtual environment is activated.

NOTE: You may have noticed in the example above that the parent directory’s basename was used in my prompt instead of the basename of the virtual environment directory itself. It does this when you name your virtual environment something generic like venv, in which case it wouldn’t really be very informative to have that in your prompt. It uses this regular expression to decide if the name is “generic”: ^\.?v(irtual)?env$

File Type Py Version Uploaded on Size
virtualenv-activator-0.1.0.tar.gz (md5) Source 2014-04-22 3KB
  • Downloads (All Versions):
  • 6 downloads in the last day
  • 62 downloads in the last week
  • 242 downloads in the last month