Setting up a Python Development Environment on OSX – Part 3 – VirtualEnv
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.
You can now create a virtual environment by “virtualenv

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

- 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.

A useful command line app to install is Yolk which allows you to list the site-packages in your environment, “pip install 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.

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.

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!

