Tuesday, November 27, 2012

Using python's virtualenv

I just finished writing a short demo application for Google Cloud Storage. To make installation of the demo easy, I wrote a setup script. Before sharing the code, I wanted to test this setup script to make sure it installs the correct packages and those packages work with my code.

My colleague suggested that I use python's virtualenv to do the testing. Doing some Google searches for virtualenv did not lead to any clear explanation of how to use it, so I thought I'd quickly jot down my own, basic experience with this tool.

(1) First, I installed virtualenv using the command:

pip install virtualenv

(2) Next, I set up a virtual environment:

virtualenv myenv

myenv, of course, can be replaced with any name you want. This command creates a directory called myenv in the current directory.

(3) To start using the new environment, I ran the following:

source myenv/bin/activate

Again, replace myenv with the name you gave your environment in step 2. After running the command, the shell indicates that I'm using myenv by adding myenv in parenthesis to the beginning of the prompt:

(myenv)kbrisbin$

(4) Since I wanted to test my setup.py code, I then ran my setup script in this new environment. The setup script successfully installed all dependencies.

(5) Once setup was complete, I ran my code to make sure everything worked.

(6) Finally, after testing, I deactivated the environment by using the deactivate command:

deactivate