pip + virtualenv
DESCRIPTION
My presentation for the very first PizzaPy.ph meetup.TRANSCRIPT
![Page 1: Pip + virtualenv](https://reader034.vdocuments.site/reader034/viewer/2022042516/5593acac1a28ab9e3f8b4612/html5/thumbnails/1.jpg)
pip + virtualenv
![Page 2: Pip + virtualenv](https://reader034.vdocuments.site/reader034/viewer/2022042516/5593acac1a28ab9e3f8b4612/html5/thumbnails/2.jpg)
More WHY? than HOW?
![Page 3: Pip + virtualenv](https://reader034.vdocuments.site/reader034/viewer/2022042516/5593acac1a28ab9e3f8b4612/html5/thumbnails/3.jpg)
What’s pip?
pip is a tool for installing and managing Python packages.
![Page 4: Pip + virtualenv](https://reader034.vdocuments.site/reader034/viewer/2022042516/5593acac1a28ab9e3f8b4612/html5/thumbnails/4.jpg)
It’s similar to yum, apt, homebrew, rubygems, etc...
![Page 5: Pip + virtualenv](https://reader034.vdocuments.site/reader034/viewer/2022042516/5593acac1a28ab9e3f8b4612/html5/thumbnails/5.jpg)
$ easy_install pip
How do you get pip?
![Page 6: Pip + virtualenv](https://reader034.vdocuments.site/reader034/viewer/2022042516/5593acac1a28ab9e3f8b4612/html5/thumbnails/6.jpg)
easy_install is a tool for installing and managing Python packages.
Wait. What’s easy_install?
![Page 7: Pip + virtualenv](https://reader034.vdocuments.site/reader034/viewer/2022042516/5593acac1a28ab9e3f8b4612/html5/thumbnails/7.jpg)
![Page 8: Pip + virtualenv](https://reader034.vdocuments.site/reader034/viewer/2022042516/5593acac1a28ab9e3f8b4612/html5/thumbnails/8.jpg)
But wait; there’s more!If you’re using a *NIX system, you can probably install pip via your OS’s package manager.
You can probably also install Python packages using your OS’s package manager.
![Page 9: Pip + virtualenv](https://reader034.vdocuments.site/reader034/viewer/2022042516/5593acac1a28ab9e3f8b4612/html5/thumbnails/9.jpg)
![Page 10: Pip + virtualenv](https://reader034.vdocuments.site/reader034/viewer/2022042516/5593acac1a28ab9e3f8b4612/html5/thumbnails/10.jpg)
So why use pip over X?easy_install:
● I honestly don’t know, but if you enjoy parroting, click here: http:
//www.pip-installer.org/en/latest/other-tools.html#pip-compared-
to-easy-install
Your OS’s package manager:
● Versions are usually outdated, especially for large projects
![Page 11: Pip + virtualenv](https://reader034.vdocuments.site/reader034/viewer/2022042516/5593acac1a28ab9e3f8b4612/html5/thumbnails/11.jpg)
Basic pip usage...# Searching...
$ pip search json
# Installing...
$ sudo pip install simplejson
# Updating…
$ sudo pip install --upgrade simplejson
# Uninstalling…
$ sudo pip uninstall simplejson
![Page 12: Pip + virtualenv](https://reader034.vdocuments.site/reader034/viewer/2022042516/5593acac1a28ab9e3f8b4612/html5/thumbnails/12.jpg)
What’s virtualenv?
virtualenv is a tool for creating isolated Python environments.
![Page 13: Pip + virtualenv](https://reader034.vdocuments.site/reader034/viewer/2022042516/5593acac1a28ab9e3f8b4612/html5/thumbnails/13.jpg)
It’s similar to chroot, FreeBSD jails, Ruby’s bundler, etc...
![Page 14: Pip + virtualenv](https://reader034.vdocuments.site/reader034/viewer/2022042516/5593acac1a28ab9e3f8b4612/html5/thumbnails/14.jpg)
Why or when do we need virtualenv?
![Page 15: Pip + virtualenv](https://reader034.vdocuments.site/reader034/viewer/2022042516/5593acac1a28ab9e3f8b4612/html5/thumbnails/15.jpg)
To answer that, we have to ask another question.
![Page 16: Pip + virtualenv](https://reader034.vdocuments.site/reader034/viewer/2022042516/5593acac1a28ab9e3f8b4612/html5/thumbnails/16.jpg)
A system-wide install!
What happens when you install something with pip?
![Page 17: Pip + virtualenv](https://reader034.vdocuments.site/reader034/viewer/2022042516/5593acac1a28ab9e3f8b4612/html5/thumbnails/17.jpg)
Why is this bad?● Only one version for the entire
machine.● You’ll end up with a lot of packages on
/usr/local● I totally made the second one up so I
can use bullet points
![Page 18: Pip + virtualenv](https://reader034.vdocuments.site/reader034/viewer/2022042516/5593acac1a28ab9e3f8b4612/html5/thumbnails/18.jpg)
Main use cases:● Different projects are going to need
different versions of packages.● Provide packages only to the projects
that need them.
![Page 19: Pip + virtualenv](https://reader034.vdocuments.site/reader034/viewer/2022042516/5593acac1a28ab9e3f8b4612/html5/thumbnails/19.jpg)
TIP!
One virtualenv for each project/repo you’re working on.
![Page 20: Pip + virtualenv](https://reader034.vdocuments.site/reader034/viewer/2022042516/5593acac1a28ab9e3f8b4612/html5/thumbnails/20.jpg)
Basic virtualenv usage...# Creating a virtualenv...
$ virtualenv ENV # creates a directory named ENV
# Activating a virtualenv
$ source bin/activate
# Manage your virtualenv’s packages
# A new virtualenv includes pip in ENV/bin/pip
...
# Leaving a virtualenv
$ source bin/deactivate
![Page 21: Pip + virtualenv](https://reader034.vdocuments.site/reader034/viewer/2022042516/5593acac1a28ab9e3f8b4612/html5/thumbnails/21.jpg)
I don’t really use virtualenv.
CONFESSION TIME
![Page 22: Pip + virtualenv](https://reader034.vdocuments.site/reader034/viewer/2022042516/5593acac1a28ab9e3f8b4612/html5/thumbnails/22.jpg)
Use virtualenvwrapper.
Vanilla virtualenv makes me want to cry.
![Page 23: Pip + virtualenv](https://reader034.vdocuments.site/reader034/viewer/2022042516/5593acac1a28ab9e3f8b4612/html5/thumbnails/23.jpg)
Basic virtualenvwrapper usage...# Creating a virtualenv...
$ mkvirtualenv env_name
# Activating a virtualenv
$ workon env_name
# Manage your virtualenv’s packages
# (install, update, uninstall, etc)
...
# Leaving a virtualenv
$ deactivate
![Page 24: Pip + virtualenv](https://reader034.vdocuments.site/reader034/viewer/2022042516/5593acac1a28ab9e3f8b4612/html5/thumbnails/24.jpg)
REMEMBER!
Everything that happens between workon and deactivate only apply to the current virtualenv.
![Page 25: Pip + virtualenv](https://reader034.vdocuments.site/reader034/viewer/2022042516/5593acac1a28ab9e3f8b4612/html5/thumbnails/25.jpg)
For virtualenvs that you can “pass around”# Save all the packages you are using to a file...
$ pip freeze > requirements.txt
# Install all packages you needed from a file...
$ pip install -r requirements.txt
# Especially useful for projects with more than one person working on it...
![Page 26: Pip + virtualenv](https://reader034.vdocuments.site/reader034/viewer/2022042516/5593acac1a28ab9e3f8b4612/html5/thumbnails/26.jpg)
Because non sequitur...
BONUS
![Page 27: Pip + virtualenv](https://reader034.vdocuments.site/reader034/viewer/2022042516/5593acac1a28ab9e3f8b4612/html5/thumbnails/27.jpg)
virtualenvs and environment variables...#!/bin/bash
# $WORKON_HOME/pizzapy/bin/postactivate
export DB_NAME=pizzapy
export DB_USER=root
export DB_PASSWORD=a1f9234a0f2cbd028
export DB_HOST=192.20.12.98
export DB_PORT=3306
![Page 28: Pip + virtualenv](https://reader034.vdocuments.site/reader034/viewer/2022042516/5593acac1a28ab9e3f8b4612/html5/thumbnails/28.jpg)
virtualenvs and environment variables...# in your Python code...
import os
DATABASES = {
‘default’: {
‘NAME’: os.environ[‘DB_NAME’]
‘USER’: os.environ[‘DB_USER’]
‘PASSWORD’: os.environ[‘DB_NAME’]
‘NAME’: os.environ[‘DB_PASSWORD’]
‘HOST’: os.environ[‘DB_HOST’]
}
}
![Page 29: Pip + virtualenv](https://reader034.vdocuments.site/reader034/viewer/2022042516/5593acac1a28ab9e3f8b4612/html5/thumbnails/29.jpg)
Questions?
END