/

12
Introduction to pip, virtualenv & & virtualenvwrapper For the First Ever Manila Python Hackers Guild Meetup pluggable, distributable & isolated python environments without the nonsense 8/8/2010 1

Transcript of /

Introduction to pip, virtualenv&&

virtualenvwrapperppFor the First Ever Manila Python Hackers Guild 

Meet‐uppluggable, distributable & isolated python environments without the 

non‐sense

8/8/2010 1

Why?Why?

• You don’t Need Sudoou do t eed Sudo• Virtualized – ‘Private’ Site‐packages

• Coupling De‐CouplingCoupling De Coupling• Organization, saving time!

• Easy to switch between versions and packages –Easy to switch between versions and packages Easy Sandbox

• Maintain clean python Stackspy• Slightly Cross‐Platform

• Eco‐Friendly ? :D

8/8/2010 2

Eco Friendly ? :D

Potential UsesPotential Uses

• Testing package upgrades / backwards‐Testing package upgrades / backwardscompatability

• Scaling development teams w/o giving root• Scaling ‐development teams w/o giving root access

R i l i l i f h• Running multiple versions of python

• Scaling – environment is portable

• Segregation of Environments (prod/stag/dev)

8/8/2010 3

Example app structure using virtualenvExample app structure using virtualenv$virtualenv (site‐packages) app source (dvcs repo)

Rx Read‐only Execute Binaries Rwx Read‐write ExecuteRx Read‐only , Execute. Binaries Rwx Read‐write, Execute, Source Code

Django==1.2.1 Your Apps

Simplejson==2 1 1 Your SourceSimplejson==2.1.1 Your Source

PIL ==1.1.7 Static Media

Reportlab==2.4

Psycopg2==2.2.1

South==0.7.1

Django‐contact‐form==0.3ja go co tact o 0 3

wsgi file

uWsgi.xml

fwww‐conf

8/8/2010 4

Complete De‐Coupled Isolation.p p

• Build nice clean python stacks.

First sudoer then, Love her and leave her it’s the python way!

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’sFor each version of python you need to install it s setuptools egg

• Can use aptitude/rpm’s. Personally I build fromCan use aptitude/rpm s. Personally I build from source (‐‐with {readline,jpeg,freetype,xslt,bzip2,xml}

8/8/2010 5

Global Python Stack ‐ SIGlobal 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 pippip 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

setuptools & pipsetuptools & pip

• Python setuptools includes easy install UsePython 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

Installing virtualenv, pip, lvirtualenvwrapper

• Visit cheeseshop and eat an eggVisit cheeseshop and eat an egg– http://pypi.python.org/pypi/setuptools– Sudo sh setuptools‐0.6c11‐py2.6.eggp py gg– Sudo easy_install pip– Sudo pip install virtualenvp p– 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

Using virtualenvUsing virtualenv

• Make a new virtualenvMake a new virtualenv– Mkvirtualenv –no‐site‐packages –python=2.x

– Automatically sources this virtualenv and itsAutomatically 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)workon (tab to see all virtualenvs)

8/8/2010 9

Using pip (not personal injury k b )protection. Ok maybe)

• Now in your virtualenv you want to load it upNow in your virtualenv you want to load it up with packages.  Handy Commands– pip search $packagenamepip search $packagename

– pip install django==0.99

– pip install –e [email protected]://$name/$reponamep p g @g //$ /$ p$branchname

– pip freeze (lists all installed packages)

– pip install –U django (upgrades)

– pip install –r requirements.txt

8/8/2010 10

Moving around virtualenvsMoving around virtualenvs

• Be out the house first (deactivate) Then weBe out the house first. (deactivate) Then we want to de‐staticize the pythonpaths– Virtualenv $env name –relocateable– Virtualenv $env_name –relocateable

– 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 andthen mkvirtualenv $name , (localizing scripts and paths.

– You’re all set.

8/8/2010 11

Further ReadingFurther 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 includinga full presentation on virtualenv, pip including download guides available for download. (posted to FB Page)Th k f d i hi i D• Thank you for enduring this pain :D

• ‐Eric Malloy, A‐Team Soln.

8/8/2010 12