Last Updated: February 25, 2016
·
2.034K
· psycojoker

How to create your custom virtualenv installation that include the packages of your choice

I use virtualenv all the time, for every new project, for every lib or project I test. I also use ipython and ipdb all the time (and django nearly all the time).

Problem: ipython, ipdb and django aren't shipped by virtualenv and installing them took some time that break my flow when I need them.

Luckily, mitsuhiko has release virtualenv-tools (https://github.com/fireteam/virtualenv-tools) that allow you to update the path of a copied virtualenv.

Some small shell scripting later and here is the result:

mkve ()
{
    # check that we have everything we need installed
    [ -e "$(which pip)" ] || (echo "installing pip"; sudo aptitude install python-pip)
    [ -e "$(which virtualenv)" ] || (echo "installing pip"; sudo pip install virtualenv)
    [ -e "$(which virtualenv-tools)" ] || (echo "installing virtualenv-tools"; sudo pip install virtualenv-tools)

    # check that we have the virtualenv ready, otherwise build it
    if [ ! -e ~/.myvirtualenv/ ]
    then
        virtualenv --distribute ~/.myvirtualenv/
        ~/.myvirtualenv/bin/pip install ipdb django django_pdb django_extensions django_debug_toolbar
    fi

    # do the actual stuff
    cp -r ~/.myvirtualenv/ ve
    virtualenv-tools --update-path $(pwd)/ve ve
    source ve/bin/activate
}

Just type mkve, it will check that you have everything needed installed then build the virtualenv if it isn't there, install the packages in it then copy this virtualenv to your current directory, update its paths and activate it.

It's even faster than to build a new virtualenv (once the global virtualenv is created).