VirtualEnv and PipIsolated Python environments Ryan Blunden Brisbane Python Group, May 2011
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
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 ofﬁce 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!
Global Package Management• Starts out ﬁne• 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
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!
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)
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 efﬁcient• Now we need the perfect package management tool for installing packages in our env...
VirtualEnvWrapper scripts• mkvirtualenv (create a new virtualenv)• rmvirtualenv (remove an existing virtualenv)• workon (change the current virtualenv)• add2virtualenv (add external packages in a .pth ﬁle 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)
Introducing pip• Pip - Pip Installs Packages, nothing more, nothing less• It’s good, James Bennet explains - http://goo.gl/F9m2N• Great at re-producing builds of speciﬁc versions of packages, just remember to use versioning for each package.• Every project needs at least one requirements ﬁle
Installing packages• By default, searches PyPi just like easy_install• Can also install from: - tarballs containing a setup.py script - Editable checkouts from VCS repos - Editable checkouts from VCS repos with revision info• Can install packages from Pip bundles
Installing packages• Installation - sudo easy_install pip• Workﬂow 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 (speciﬁc) pip install package-name>=1.4.5 (at least...)• Install packages from requirements ﬁle pip install -r requirements.txt• Freeze current packages to produce requirements ﬁle
Terminal action!• Finally, let’s write some code• For those watching at home, go checkout Mathemtism’s excellent screen cast at http://goo.gl/wWkti
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.
Summary• Isolated Python environments are a must• VirtualEnv,VirtualEnvWrapper are THE way to manage them
References• Pip and VirtualEnv (Awesome!) http://mathematism.com/2009/07/30/presentation-pip- and-virtualenv/• Tools of the Modern Python Hacker:Virtualenv, Fabric and Pip http://www.clemesha.org/blog/modern-python-hacker- tools-virtualenv-fabric-pip• Notes on using pip and virtualenv with Django http://www.saltycrane.com/blog/2009/05/notes-using-pip- and-virtualenv-django/
Thanks!• Ryan Blunden email@example.com @ryan_blunden