Pip, Virtualenv, VirtualenvWrapper & More Django District Wednesday, Nov. 7, 2012 Jackie Kazil @jackiekazil
May 08, 2015
Pip, Virtualenv, VirtualenvWrapper &
MoreDjango 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 405Python 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.shexport 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 djangopip install -r requirements.txtpip install -U django
Where does it live? ~/.virtualenv/foo/...
…/binBinaries in your env. Example: ipython
.../includeDev headers used by the #include directive in C.
…/libWhere all your python libraries will livelib/python2.7/site-packages
.../shareMans & 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_MODULEfi
Hooks example: postmkvirtualenv
https://gist.github.com/4034103
Other helpful stuff
pip freezepip 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
Autoenvhttps://github.com/kennethreitz/autoenv"Magic per-project shell environments. Very pretentious."
Bundle Scouthttp://bundlescout.com/"Daily emails keep you on top of the latest updates from your favorite libraries."
More .... Keeping things fresh (cont)
Pip Linthttps://github.com/dcramer/piplint"Piplint validates your current environment with the requirements files you've specified."
Pip Toolshttps://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/