Virtualenv

2,185 views
2,110 views

Published on

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

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,185
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
29
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Virtualenv

  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>http://pypi.python.org/pypi/setuptools#installation-instructions </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/virtualenvwrapper.sh </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: http://pypi.python.org/pypi/virtualenv
  33. 33. virtualenvwrapper: http://www.doughellmann.com/docs/virtualenvwrapper/
  34. 34. pip: http://www.pip-installer.org/en/latest/index.html </li></ul>
  35. 35. Acknowledgements <ul><li>Some ideas drawn from: Introduction to pip, virtualenv & virtualenvwrapper by Eric Malloy @ A-team Solutions
  36. 36. http://www.slideshare.net/ateamsolution/httpwwwslidesharenetconfirmmjy1ntq0mja7zxjpy0a </li></ul>
  37. 37. Q & A <ul><li>Contact me at: [email_address]
  38. 38. Presentation at: http://www.slideshare.net/jnials/virtualenv-9208950 </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 http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA. </li></ul>

×