VirtualEnv and Pip - Isolated Python environments


Published on

Creating completely separate and isolated Python environments for each project is essential. Pip and VirtualEnv make it easy!

Published in: Education, Technology

VirtualEnv and Pip - Isolated Python environments

  1. 1. VirtualEnv and PipIsolated Python environments Ryan Blunden Brisbane Python Group, May 2011
  2. 2. Summary• Global site-packages management• Per project package management• VirtualEnv creates isolated Py envs• VirtualEnvWrapper makes things easy• Pip to install your packages• Next... Automate your deploys
  3. 3. About Me• Have been the Front End Tech Lead at Flight Centre• Interested in all things web and am a fan of the new RESTful back-end => thick client movement• Python at work for back office and Systems Admin tasks. Don’t get why SA’s love bash scripts so much• Can’t build Django apps at work so I build them for myself and external clients• I’m hardly a Python expert, but I love it!
  4. 4. Global Package Management• Starts out fine• Overtime, things get messy• Not great for development in teams (sync issues)• Challenges in keeping dev, stage and prod environments in sync• Sometimes you don’t even have control of the prod global site packages• Easy for things to break overtime when versions and dependencies not clearly set
  5. 5. Per Project Packages• Enter virtualenv, a tool for creating isolated Python environments• Better than workingenv as it doesn’t rely on $PYTHONPATH trickery• Better than virtual-python as it works on Windows (doesn’t rely on symlinks)• Any of these are better than nothing, I just think virtualenv does it best.• Version 1.6.1 supports PyPy 1.5. PyPy!
  6. 6. Installing virtualenv• sudo easy_install virtualenv, then add this to your .bashrc• Create a new Python environment • virtualenv myproject --no-site-packages Don’t inherit from global site-packages directory • virtualenv myproject --no-site-packages --distribute Uses distribute instead of setup_tools • I think it’s best to ensure your new env does not inherit packages from your global site-packages directory• This is ok but to make things easier to manage, let’s use virtualenvwrapper (Thanks Doug Hellmann)
  7. 7. virtualenvwrapper• Organises your virtualenvs into one spot # Put this in your .bash_profile or .bashrc export WORKON_HOME=$HOME/.virtualenvs source /usr/local/bin/virtualenvwrapper_bashrc• Separates your virtualenvs from your project source• Nice environment event hooks for automation of repetitive tasks (cd project on post_activate)• Basically, it makes your use of virtualenv more efficient• Now we need the perfect package management tool for installing packages in our env...
  8. 8. VirtualEnvWrapper scripts• mkvirtualenv (create a new virtualenv)• rmvirtualenv (remove an existing virtualenv)• workon (change the current virtualenv)• add2virtualenv (add external packages in a .pth file to current virtualenv)• cdsitepackages (cd into the site-packages directory of current virtualenv)• cdvirtualenv (cd into the root of the current virtualenv)• deactivate (deactivate virtualenv, which calls several hooks)
  9. 9. Virtual EnvWrapper Hooks• postmkvirtualenv• prermvirtualenv• postrmvirtualenv• postactivate• predeactivate• postdeactivate
  10. 10. Introducing pip• Pip - Pip Installs Packages, nothing more, nothing less• It’s good, James Bennet explains -• Great at re-producing builds of specific versions of packages, just remember to use versioning for each package.• Every project needs at least one requirements file
  11. 11. Installing packages• By default, searches PyPi just like easy_install• Can also install from: - tarballs containing a script - Editable checkouts from VCS repos - Editable checkouts from VCS repos with revision info• Can install packages from Pip bundles
  12. 12. Installing packages• Installation - sudo easy_install pip• Workflow 1. Make or activate virtual env 2. Install (and search for) packages• pip search pip install package-name (latest) pip install package-name==1.4.5 (specific) pip install package-name>=1.4.5 (at least...)• Install packages from requirements file pip install -r requirements.txt• Freeze current packages to produce requirements file
  13. 13. Terminal action!• Finally, let’s write some code• For those watching at home, go checkout Mathemtism’s excellent screen cast at
  14. 14. What about my editor?• Good Python editors and IDE’s should support virtual env• PyCharm does by you telling it which python interpreter should be used for each project, it then nows which site packages directory to use.
  15. 15. Summary• Isolated Python environments are a must• VirtualEnv,VirtualEnvWrapper are THE way to manage them
  16. 16. Docs• VirtualEnv• VirtualEnvWrapper• Pip
  17. 17. References• Pip and VirtualEnv (Awesome!) and-virtualenv/• Tools of the Modern Python Hacker:Virtualenv, Fabric and Pip tools-virtualenv-fabric-pip• Notes on using pip and virtualenv with Django and-virtualenv-django/
  18. 18. Thanks!• Ryan Blunden @ryan_blunden