1. Introduction to pip, virtualenv
&
virtualenvwrapper
pp
For the First Ever Manila Python Hackers Guild
Meet‐up
pluggable, distributable & isolated
python environments without the
non‐sense
8/8/2010 1
2. Why?
• You don’t Need Sudo
ou do t eed Sudo
• Virtualized – ‘Private’ Site‐packages
• Coupling De‐Coupling
Coupling De Coupling
• Organization, saving time!
• Easy to switch between versions and packages –
Easy to switch between versions and packages
Easy Sandbox
• Maintain clean python Stacks
py
• Slightly Cross‐Platform
• Eco‐Friendly ? :D
Eco Friendly ? :D
8/8/2010 2
3. Potential Uses
Potential Uses
• Testing package upgrades / backwards‐
Testing package upgrades / backwards
compatability
• Scaling development teams w/o giving root
Scaling ‐development teams w/o giving root
access
• R
Running multiple versions of python
i li l i f h
• Scaling – environment is portable
• Segregation of Environments (prod/stag/dev)
8/8/2010 3
4. Example app structure using virtualenv
Example app structure using virtualenv
$virtualenv (site‐packages) app source (dvcs repo)
Rx Read‐only , Execute. Binaries
Rx Read‐only Execute Binaries Rwx Read‐write Execute
Read‐write, Execute,
Source Code
Django==1.2.1 Your Apps
Simplejson==2.1.1
Simplejson==2 1 1 Your Source
Your Source
PIL ==1.1.7 Static Media
Reportlab==2.4
Psycopg2==2.2.1
South==0.7.1
ja go co tact o 03
Django‐contact‐form==0.3
wsgi file
uWsgi.xml
www‐conf
f
8/8/2010 4
5. Complete De‐Coupled Isolation.
p p
First sudoer then, Love her and leave her it’s the python way!
• Build nice clean python stacks.
Build nice clean python stacks.
• This is the one and only time you need sudo!
• For each version of python you need to install it’s
For each version of python you need to install it s
setuptools egg
• Can use aptitude/rpm’s. Personally I build from
Can use aptitude/rpm s. Personally I build from
source (‐‐with {readline,jpeg,freetype,xslt,bzip2,xml}
8/8/2010 5
6. Global Python Stack
Global Python Stack ‐ SI
Python2.5 Python2.6 Python2.7 Python3.1
setuptools setuptools setuptools setuptools
Easy_install Easy_install Easy_install Easy_install
pip pip pip pip
virtualenv virtualenv virtualenv virtualenv
virtualenvwrapper virtualenvwrapper virtualenvwrapper virtualenvwrapper
This is the bare minimum, all you need. The rest of the packages can go in the
virtualenv
8/8/2010 6
7. setuptools & pip
& pip
• Python setuptools includes easy install Use
Python setuptools includes easy_install. Use
her to get pip, then loose her.
• pip is the new easy install
pip is the new easy_install.
• Use pip because
– virtualenv & dvcs aware
– Can uninstall
– Requirements files
– No partial installs & automatic dependency install
8/8/2010 7
8. Installing virtualenv, pip,
virtualenvwrapper
l
• Visit cheeseshop and eat an egg
Visit cheeseshop and eat an egg
– http://pypi.python.org/pypi/setuptools
– Sudo sh setuptools‐0.6c11‐py2.6.egg
p py gg
– Sudo easy_install pip
– Sudo pip install virtualenv
pp
– Sudo pip install virtualenvwrapper
– Edit ~/.bash_aliases, put last line source
/usr/local/bin/virtualenvwrapper.sh
– Rehash. Good to go. Leave sudo
8/8/2010 8
9. Using virtualenv
Using virtualenv
• Make a new virtualenv
Make a new virtualenv
– Mkvirtualenv –no‐site‐packages –python=2.x
– Automatically sources this virtualenv and its
Automatically sources this virtualenv and its
PYTHONPATH
– Checkout all the bash scripts in ~/.virtualenvs/$name
• Get out of virtualenv
– deactivate
• Activate virtualenv (tab completion!!!)
– workon (tab to see all virtualenvs)
(tab to see all virtualenvs)
8/8/2010 9
10. Using pip (not personal injury
protection. Ok maybe)
k b )
• Now in your virtualenv you want to load it up
Now in your virtualenv you want to load it up
with packages. Handy Commands
– pip search $packagename
pip search $packagename
– pip install django==0.99
– pip install –e git@github.com://$name/$reponame
pp g @g //$ /$ p
$branchname
– pip freeze (lists all installed packages)
– pip install –U django (upgrades)
– pip install –r requirements.txt
8/8/2010 10
11. Moving around virtualenvs
Moving around virtualenvs
• Be out the house first (deactivate) Then we
Be out the house first. (deactivate) Then we
want to de‐staticize the pythonpaths
– Virtualenv $env name –relocateable
$env_name
– Tar her up (tar cjvf $name.tar.bz2
~/ virtualenvs/$name) transfer her to new server
/.virtualenvs/$name), transfer her to new server.
– Extract archive to path (~/.virtualenvs/$name)
then mkvirtualenv $name , (localizing scripts and
then mkvirtualenv $name , (localizing scripts and
paths.
– You’re all set.
8/8/2010 11
12. Further Reading
Further Reading
• Tools of the Modern Python Hacker: virtualenv,
y ,
fabric, and pip. http://bit.ly/3JkKTX
• Always check the cheeseshop for documenation.
(http://pypi.python.org
(http //pypi python org
• Just in time for Septembers Meet‐up we will have
a full presentation on virtualenv, pip including
a full presentation on virtualenv, pip including
download guides available for download. (posted
to FB Page)
• Th k
Thank you for enduring this pain :D
f d i hi i D
• ‐Eric Malloy, A‐Team Soln.
8/8/2010 12