Skip to main content

A better activate script for Python's virtualenv

Project description

The default activate script for Python’s virtualenv 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.

This package provides a replacement activate script that 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$

Simple Mode

Simple mode is enabled via the VIRTUALENV_ACTIVATOR_SIMPLE environment variable. When this variable is set, the activator doesn’t try to restore your environment to its previous state. It doesn’t even provide a deactivate function. This is sometimes more desirable when running a daemon for instance. Here’s an example of a bash script you might write to wrap a daemon written in Python so that the virtual environment is automatically activated for the daemon:

. /path/to/venv/etc/
my_daemon "$@"

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

virtualenv-activator-0.1.2.tar.gz (4.0 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page