le_bebna_kamni: (Monk)
[personal profile] le_bebna_kamni
I am finally free of the tyranny that is virtualenv. Don't get me wrong -- it's marvelous tool for working in multiple Python development environments that all have different requirements. For those of you not familiar with virtualenv it does all these wonderful things:
  1. allows you to install all your dependencies without being root

  2. lets you have a clean working environment with only the dependencies you need -- you don't have problems with conflicting dependencies or giving your project to someone and forgetting to mention a dependency because you previously had it installed on your machine

  3. easy uninstall -- just delete the folder

  4. adds to your geek cred

But it creates a slew of virtualenv folders that are hard to manage and can't be moved except at great cost to time and sanity (all hail Cthulhu). It also creates that annoying trail of folders that looks something like 'foo/foo/foo' (virtualenv/Django project/Django app)*.

In steps virtualenvwrapper. This beautiful tool puts all your virtualenvs into your home directory into a hidden folder that you never have to think about unless you want to do some special configuration. Making a virtualenv using wrapper is a simple 'mkvirtualenv --no-site-packages [project name]', and then you can place your actual project directory anywhere you want (and easily move it later without breaking it). To work in your virtualenv, you simply type 'workon [project name]' from any directory, and you're instantly in your Python environment.

And speaking of special configuration... you can create scripts to run before and after you make, activate, deactivate, or remove a virtualenv. And you can configure these globally or on a per-project basis. I'm fairly modest with my scripts right now -- a simple cd into the project directory when I activate, then a cd back to wherever I started from. To do this, you need to edit the following two files:

~/.virtualenvs/[project name]/bin/postactivate:
export ORIGDIR=`pwd`
cd /path/to/project/folder
~/.virtualenv/postdeactivate:
cd $ORIGDIR


Notes on configuration:


Before you can make your first wrapped virtualenv, you have to create a .virtualenv folder to house your many environments; otherwise virtualenvwrapper will complain at you. This is probably the biggest shortcoming (and only shortcoming, so far for me) that virtualenvwrapper has. Why it can't attempt to auto-create the folder and *then* complain if it can't, is beyond me. Fortunately you only have to deal with this problem once.

You'll need to source the virtualenv.sh script before you can use any commands. For those Ubuntu users out there, the easiest way for this to happen automagically is to edit your .bashrc file to have the following line:
source /usr/local/bin/virtualenvwrapper.sh
I think it's .bash_profile for Mac users -- double-check the install location of virtualenvwrapper.sh; Windows users, you're on your own. ;)

---------------------------------------------

* Okay, you can name the folders foo-env/foo-proj/foo, but that still annoys me after awhile, especially if you have long project names to begin with.

** For those of you who don't know, I now have a new job doing Python development. I'm really *squee*-ed about it. Unfortunately it involves moving halfway across the country, in exactly two weeks. Right now I'm telecommuting, planning a move, and trying to finish the contract work I picked up here in Michigan to tide me over until I found a job. Hence why I'm working (and procrastinating) on a holiday. :)

on 2010-11-26 04:42 am (UTC)
Posted by [identity profile] delosd.livejournal.com
I heard about the move, and have been meaning to get hold of you. I know we haven't seen each other much lately, but I'm going to miss you! At least I'll still know where to find you. On line. :)

on 2011-04-11 09:09 am (UTC)
Posted by [identity profile] henaiota.livejournal.com
Terrific work! This is the type of information that should be shared around the web. Shame on the search engines for not positioning this post higher!

on 2011-04-12 07:15 pm (UTC)
Posted by [identity profile] henaiota.livejournal.com
Thanks for posting, I like that_or_not blog!

on 2011-11-02 02:28 am (UTC)
Posted by [identity profile] langanaxor.livejournal.com
Hi there, awesome site. I thought the topics you posted on were very interesting

Profile

le_bebna_kamni: (Default)
le_bebna_kamni

April 2017

S M T W T F S
      1
2345678
9101112131415
16 171819202122
23242526272829
30      

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jul. 25th, 2017 10:49 am
Powered by Dreamwidth Studios