pip + virtualenv
More WHY?
than HOW?
What’s pip?
pip is a tool for installing and
managing Python packages.
It’s similar to yum,
apt, homebrew,
rubygems, etc...
How do you get pip?
$ easy_install pip
Wait. What’s easy_install?

easy_install is a tool for
installing and managing
Python packages.
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.
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-comparedto-easy-install
Your OS’s package manager:
● Versions are usually outdated, especially for large projects
Basic pip usage...
# Searching...
$ pip search json
# Installing...
$ sudo pip install simplejson
# Updating…
$ sudo pip install --upgrade simplejson
# Uninstalling…
$ sudo pip uninstall simplejson
What’s virtualenv?

virtualenv is a tool for
creating isolated Python
environments.
It’s similar to chroot,
FreeBSD jails, Ruby’s
bundler, etc...
Why or when do
we need
virtualenv?
To answer that, we
have to ask another
question.
What happens when
you install
something with pip?
A system-wide install!
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
Main use cases:
● Different projects are going to need
different versions of packages.
● Provide packages only to the projects
that need them.
One virtualenv for
each project/repo
you’re working on.
TIP!
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
CONFESSION TIME

I don’t really use virtualenv.
Use virtualenvwrapper.
Vanilla virtualenv makes
me want to cry.
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
Everything that happens
between workon and
deactivate only apply to
the current virtualenv.
REMEMBER!
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...
BONUS
Because non sequitur...
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
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’]
}
}
END
Questions?

Pip + virtualenv

  • 1.
  • 2.
  • 3.
    What’s pip? pip isa tool for installing and managing Python packages.
  • 4.
    It’s similar toyum, apt, homebrew, rubygems, etc...
  • 5.
    How do youget pip? $ easy_install pip
  • 6.
    Wait. What’s easy_install? easy_installis a tool for installing and managing Python packages.
  • 8.
    But wait; there’smore! 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.
  • 10.
    So why usepip 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-comparedto-easy-install Your OS’s package manager: ● Versions are usually outdated, especially for large projects
  • 11.
    Basic pip usage... #Searching... $ pip search json # Installing... $ sudo pip install simplejson # Updating… $ sudo pip install --upgrade simplejson # Uninstalling… $ sudo pip uninstall simplejson
  • 12.
    What’s virtualenv? virtualenv isa tool for creating isolated Python environments.
  • 13.
    It’s similar tochroot, FreeBSD jails, Ruby’s bundler, etc...
  • 14.
    Why or whendo we need virtualenv?
  • 15.
    To answer that,we have to ask another question.
  • 16.
    What happens when youinstall something with pip? A system-wide install!
  • 17.
    Why is thisbad? ● 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
  • 18.
    Main use cases: ●Different projects are going to need different versions of packages. ● Provide packages only to the projects that need them.
  • 19.
    One virtualenv for eachproject/repo you’re working on. TIP!
  • 20.
    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
  • 21.
    CONFESSION TIME I don’treally use virtualenv.
  • 22.
  • 23.
    Basic virtualenvwrapper usage... # Creatinga virtualenv... $ mkvirtualenv env_name # Activating a virtualenv $ workon env_name # Manage your virtualenv’s packages # (install, update, uninstall, etc) ... # Leaving a virtualenv $ deactivate
  • 24.
    Everything that happens betweenworkon and deactivate only apply to the current virtualenv. REMEMBER!
  • 25.
    For virtualenvs thatyou 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...
  • 26.
  • 27.
    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
  • 28.
    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’] } }
  • 29.