Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.



Published on

Presentation at PyTexas 2011 on virtualenv, pip, and virtualenvwrapper.

Published in: Technology
  • Be the first to comment


  1. 1. virtualenv, pip & virtualenvwrapper OR Saving your sanity while working with multiple python projects
  2. 2. Why Virtualize our Environments? <ul><li>Allows us to isolate our environments (Sandbox).
  3. 3. Decouples our development environment from our system.
  4. 4. You don't need sudo.
  5. 5. It's agile (buzzword compliant).
  6. 6. Easy to test different versions. </li></ul>
  7. 7. Getting virtualized <ul><li>Install setuptools if you don't already have easy_install. </li></ul><ul><ul><li> </li></ul></ul><ul><li>Install pip, virtualenv, & virtualenvwrapper </li></ul><ul><ul><li>sudo easy_install pip
  8. 8. sudo pip install virtualenv
  9. 9. sudo pip install virtualenvwrapper
  10. 10. Edit $HOME/.bashrc and add the following line: source /usr/local/bin/ </li></ul></ul>
  11. 11. Setting up your first virtual environment <ul><li>It's really hard. </li></ul><ul><ul><li>mkvirtualenv first </li></ul></ul><ul><li>Not hard enough? </li></ul><ul><ul><li>mkvirtualenv –python=python2.7 –no-site-packages second </li></ul></ul><ul><li>That better? </li></ul>
  12. 12. Adding Libraries <ul><li>You add libraries with pip </li></ul><ul><ul><li>pip install boto
  13. 13. pip install -r requirements.txt
  14. 14. pip install -U django (upgrades)
  15. 15. pip install -e (repo path – See docs) </li></ul></ul>
  16. 16. Working inside your virtualenv <ul><li>Within your environment you simply use the python on the path. </li></ul><ul><ul><li>$ type python python is /home/jnials/.virtualenvs/first/bin/python </li></ul></ul><ul><li>Lets not forget </li></ul><ul><ul><li>lssitepackages </li></ul></ul>
  17. 17. Changing virtual environments <ul><li>You can turn on or switch between environments with 'workon': </li></ul><ul><ul><li>workon first
  18. 18. workon second </li></ul></ul><ul><li>You leave the virtual environment completely with 'deactivate' </li></ul><ul><ul><li>deactivate </li></ul></ul>
  19. 19. Manipulating virtual environments <ul><li>List your virtual environments with: </li></ul><ul><ul><li>lsvirtualenv </li></ul></ul><ul><li>And then remove them with: </li></ul><ul><ul><li>rmvirtualenv first </li></ul></ul>
  20. 20. Customizing virtual environments <ul><li>You can do it within virtualenv itself, but it is better to do it within virtualenvwrapper
  21. 21. Some environment variables you can set: </li></ul><ul><ul><li>WORKON_HOME, VIRTUALENVWRAPPER_HOOK_DIR, VIRTUALENV_USE_DISTRIBUTE </li></ul></ul><ul><li>$VIRTUALENVWRAPPER_HOOK_DIR holds a number of hook scripts that can be used to customize your virtual environments. </li></ul>
  22. 22. postmkvirtualenv <ul>#!/bin/bash # This hook is run after a new virtualenv is activated. toggleglobalsitepackages pip install ipython pip install unittest2 pip install pylint </ul>
  23. 23. The joys of pip <ul><li>pip is just better than easy_install
  24. 24. pip freeze can generate a requirements file that can be read by pip install -r
  25. 25. pip can search the python package index (PyPi)
  26. 26. Supports uninstallation of packages.
  27. 27. Supports git, hg, bzr.
  28. 28. Easier to type. </li></ul>
  29. 29. Neat Features & Plugins <ul><li>Templates </li></ul><ul><ul><li>Bitbucket – automatically clones a mercurial repo
  30. 30. Django – automatically creates a django project </li></ul></ul><ul><li>Extensions </li></ul><ul><ul><li>user_scripts – enables the customization features </li></ul></ul><ul><li>Religious Features </li></ul><ul><ul><li>Emacs-desktop – emacs support
  31. 31. Vim-virtualenv – vi support </li></ul></ul>
  32. 32. Find out more <ul><li>virtualenv:
  33. 33. virtualenvwrapper:
  34. 34. pip: </li></ul>
  35. 35. Acknowledgements <ul><li>Some ideas drawn from: Introduction to pip, virtualenv & virtualenvwrapper by Eric Malloy @ A-team Solutions
  36. 36. </li></ul>
  37. 37. Q & A <ul><li>Contact me at: [email_address]
  38. 38. Presentation at: </li></ul>
  39. 39. License <ul><li>This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. To view a copy of this license, visit or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA. </li></ul>