Setting up a Python Development Environment on OSX – Part 3 – VirtualEnv

By , August 13, 2011

This is part 3 of a series on setting up a great Python development environment on OSX. Part 1 can be found here and Part 2 can be found here and Part 4 can be found here. This part focuses on getting virtualenv installed so that you can create and use development projects that have isolated instances of the Python interpreter, site-packages and other local configurations. When combined with pip requirements.txt files this allows you to rapidly build up and share custom project environments and rapidly create test setups (see later post).

To install virtualenv use “sudo easy_install” (yes I forgot my password for a second !). You can just as easily use pip.

Virtualenv

You can now create a virtual environment by “virtualenv ” in this case blogtest.

Envname

You will now have a new folder created along with three sub-folders, bin, include and lib.

Ls

  • bin – contains the Python interpreter and some scripts to activate your environment
  • include – contains the imported packages
  • lib – used for the libraries including where your site-packages for this environment will be stored

To activate your environment you source /bin/activate. You will notice that your command prompt now shows the active environment.

Env setup

A useful command line app to install is Yolk which allows you to list the site-packages in your environment, “pip install yolk”

Yolk

and “yolk -l” will list the site-packages in the environment. At this point you will see that while we now have the ability too create a new isolated environment it was created with a LOT of baggage.

Yolklwithcrap

Luckily virtualenv has a simple solution to this, so lets now create a new clean environment using the –no-site-packages switch.

“virtualenv blogtest2 –no-site-packages”, activate the environment using “source blogtest2/bin/activate” and install yolk using “pip install yolk”. A “yolk -l” will now show a clean new environment.

Yolkl

All very cool but actually those with a sharp eye will have noticed the real power of virtualenv. If you had tried to install yolk not in a virtualenv (either you forgot to activate the env you just created wanted it system wide) you would have had to have used sudo and provided root credentials. Virtualenv is installing local packages and so doesn’t require root permissions. This is a super handy catch-net when you are playing with settings and environments but scared of making a change that will break your development machine.

Next up in the series configuring Pycharm including getting it to work with your virtualenv’s and installing some useful extensions and then onto the final post in the series which pulls it all together to create a solid end-to-end project using all the utilities and lessons learned!

Share on TwitterSubmit to reddit

Panorama Theme by Themocracy