Pip, Virtualenv,
VirtualenvWrapper &
        More
      Django District
  Wednesday, Nov. 7, 2012

                        Jackie Kazil
                        @jackiekazil
Where we all start
● easy_install Globally
● And it works! Yay!
● But then....
  ○ dependencies or versions
  ○ coordination w/ other developers
  ○ keeping different systems on same page
  ○ breaking sys python
Case study: Dependencies
Once upon a time in city, much like this one,
there was a newspaper...

     ….. over 90 Django applications updated
  from 0.96 to 1.1.
Case study: Breaking sys python
From stackoverflow...
"Is there a way to ensure I can clean everything
up and start from zero?"

"If you remove everything in these 2 directories,
it should clear out all modules...."
Why Pip & Virtualenv
● Isolate
  ○ System packages from project packages
  ○ Projects from each other

● No sudo required - sys python requires sudo
  access, project python doesn’t
Becoming the standard...
Pep 405
Python 3.3

http://www.python.org/dev/peps/pep-0405

"... proposes to add to Python a mechanism for
lightweight "virtual environments" with their own
site directories, optionally isolated from system
site directories...."
What is...
●   pip - replaces easy_install

●   virtualenv - creates isolated python
    environments




●   virtualenvwrapper - to be explained later
Basic setup
$ sudo easy_install pip
$ sudo pip install virtualenv
$ sudo pip install virtualenvwrapper

Add to bash settings...
source /usr/local/bin/virtualenvwrapper.sh
export WORKON_HOME=$HOME/.virtualenvs
Make environment
$ mkvirtualenv foo

You can add more to this... like setting python
version.
$ mkvirtualenv --python=python2.7
What is virtualenvwrapper?
Helps make things easier...

  workon foo
    vs
  source ~/.virtualenvs/foo/bin/activate
Installing libraries
pip install django
pip install -r requirements.txt
pip install -U django
Where does it live? ~/.virtualenv/foo/...
…/bin
  Binaries in your env. Example: ipython

.../include
     Dev headers used by the #include directive in C.

…/lib
   Where all your python libraries will live
   lib/python2.7/site-packages

.../share
     Mans & Docs
Wrappers
●   mkvirtualenv (calls hooks)
●   rmvirtualenv
●   workon (calls hooks)
●   add2virtualenv
●   cdsitepackages
●   cdvirtualenv
●   deactivate (calls hooks)
Hooks
●   postmkvirtualenv
●   prermvirtualenv
●   postrmvirtualenv
●   postactivate
●   predeactivate
●   postdeactivate
Hook example: postactivate
proj_name=$(echo $VIRTUAL_ENV|awk -F'/' '{print $NF}')

if [ $proj_name = "foo" ]; then
    cd /opt/foo/core/
    export DJANGO_SETTINGS_MODULE='settings.
dev_settings'
    echo "DJANGO_SETTINGS_MODULE set to:"
$DJANGO_SETTINGS_MODULE
fi
Hooks example: postmkvirtualenv
https://gist.github.com/4034103
Other helpful stuff
pip freeze
pip freeze > requirements.txt

yolk -l

python -c "import sys,pprint;pprint.pprint(sys.
path)"
More... virtualenv-burrito
https://github.com/brainsik/virtualenv-burrito

With one command.... BAM! (or maybe not)
... a virtualenv & virtualenvwrapper environment
More... virtualenv-tools
https://github.com/fireteam/virtualenv-tools

"This repository contains scripts we're
using at Fireteam for our deployment of
Python code."
More... Tox http://tox.testrun.org/latest/
Tox as is a generic virtualenv management and test
command line tool you can use for:
● checking your package installs correctly with different
   Python versions and interpreters

● running your tests in each of the environments,
  configuring your test tool of choice

● acting as a frontend to Continuous Integration servers,
  greatly reducing boilerplate and merging CI and shell-
  based testing.
More .... Keeping things fresh
Autoenv
https://github.com/kennethreitz/autoenv
"Magic per-project shell environments. Very pretentious."


Bundle Scout
http://bundlescout.com/
"Daily emails keep you on top of the latest updates from
your favorite libraries."
More .... Keeping things fresh
(cont)
Pip Lint
https://github.com/dcramer/piplint
"Piplint validates your current environment with the
requirements files you've specified."


Pip Tools
https://github.com/nvie/pip-tools
"A set of two command line tools to help you keep your pip-
based packages fresh, even when you've pinned them"
The End
Me: Jackie Kazil, @jackiekazil

W/ contributions from...
Chris Adams, @acdha
   &
Members of Django-District :-)
http://www.django-district.org/

Django district pip, virtualenv, virtualenv wrapper & more

  • 1.
    Pip, Virtualenv, VirtualenvWrapper & More Django District Wednesday, Nov. 7, 2012 Jackie Kazil @jackiekazil
  • 2.
    Where we allstart ● easy_install Globally ● And it works! Yay! ● But then.... ○ dependencies or versions ○ coordination w/ other developers ○ keeping different systems on same page ○ breaking sys python
  • 3.
    Case study: Dependencies Onceupon a time in city, much like this one, there was a newspaper... ….. over 90 Django applications updated from 0.96 to 1.1.
  • 4.
    Case study: Breakingsys python From stackoverflow... "Is there a way to ensure I can clean everything up and start from zero?" "If you remove everything in these 2 directories, it should clear out all modules...."
  • 5.
    Why Pip &Virtualenv ● Isolate ○ System packages from project packages ○ Projects from each other ● No sudo required - sys python requires sudo access, project python doesn’t
  • 6.
    Becoming the standard... Pep405 Python 3.3 http://www.python.org/dev/peps/pep-0405 "... proposes to add to Python a mechanism for lightweight "virtual environments" with their own site directories, optionally isolated from system site directories...."
  • 7.
    What is... ● pip - replaces easy_install ● virtualenv - creates isolated python environments ● virtualenvwrapper - to be explained later
  • 8.
    Basic setup $ sudoeasy_install pip $ sudo pip install virtualenv $ sudo pip install virtualenvwrapper Add to bash settings... source /usr/local/bin/virtualenvwrapper.sh export WORKON_HOME=$HOME/.virtualenvs
  • 9.
    Make environment $ mkvirtualenvfoo You can add more to this... like setting python version. $ mkvirtualenv --python=python2.7
  • 10.
    What is virtualenvwrapper? Helpsmake things easier... workon foo vs source ~/.virtualenvs/foo/bin/activate
  • 11.
    Installing libraries pip installdjango pip install -r requirements.txt pip install -U django
  • 12.
    Where does itlive? ~/.virtualenv/foo/... …/bin Binaries in your env. Example: ipython .../include Dev headers used by the #include directive in C. …/lib Where all your python libraries will live lib/python2.7/site-packages .../share Mans & Docs
  • 13.
    Wrappers ● mkvirtualenv (calls hooks) ● rmvirtualenv ● workon (calls hooks) ● add2virtualenv ● cdsitepackages ● cdvirtualenv ● deactivate (calls hooks)
  • 14.
    Hooks ● postmkvirtualenv ● prermvirtualenv ● postrmvirtualenv ● postactivate ● predeactivate ● postdeactivate
  • 15.
    Hook example: postactivate proj_name=$(echo$VIRTUAL_ENV|awk -F'/' '{print $NF}') if [ $proj_name = "foo" ]; then cd /opt/foo/core/ export DJANGO_SETTINGS_MODULE='settings. dev_settings' echo "DJANGO_SETTINGS_MODULE set to:" $DJANGO_SETTINGS_MODULE fi
  • 16.
  • 17.
    Other helpful stuff pipfreeze pip freeze > requirements.txt yolk -l python -c "import sys,pprint;pprint.pprint(sys. path)"
  • 18.
    More... virtualenv-burrito https://github.com/brainsik/virtualenv-burrito With onecommand.... BAM! (or maybe not) ... a virtualenv & virtualenvwrapper environment
  • 19.
    More... virtualenv-tools https://github.com/fireteam/virtualenv-tools "This repositorycontains scripts we're using at Fireteam for our deployment of Python code."
  • 20.
    More... Tox http://tox.testrun.org/latest/ Toxas is a generic virtualenv management and test command line tool you can use for: ● checking your package installs correctly with different Python versions and interpreters ● running your tests in each of the environments, configuring your test tool of choice ● acting as a frontend to Continuous Integration servers, greatly reducing boilerplate and merging CI and shell- based testing.
  • 21.
    More .... Keepingthings fresh Autoenv https://github.com/kennethreitz/autoenv "Magic per-project shell environments. Very pretentious." Bundle Scout http://bundlescout.com/ "Daily emails keep you on top of the latest updates from your favorite libraries."
  • 22.
    More .... Keepingthings fresh (cont) Pip Lint https://github.com/dcramer/piplint "Piplint validates your current environment with the requirements files you've specified." Pip Tools https://github.com/nvie/pip-tools "A set of two command line tools to help you keep your pip- based packages fresh, even when you've pinned them"
  • 23.
    The End Me: JackieKazil, @jackiekazil W/ contributions from... Chris Adams, @acdha & Members of Django-District :-) http://www.django-district.org/