Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to use PIP to install Python packages programmatically.py
#!/usr/bin/env python
"""
PIP can stand for PIP Installs packages Programmatically, too!
(and here's how)
Requires:
* Python 2.6 or higher (for print-as-a-function)
* PIP 0.8.2
"""
from __future__ import print_function
from pip.index import PackageFinder
from pip.req import InstallRequirement, RequirementSet
from pip.locations import build_prefix, src_prefix
requirement_set = RequirementSet(
build_dir=build_prefix,
src_dir=src_prefix,
download_dir=None)
requirement_set.add_requirement( InstallRequirement.from_line("django-hoptoad", None) )
install_options = []
global_options = []
finder = PackageFinder(find_links=[], index_urls=["http://pypi.python.org/simple/"])
requirement_set.prepare_files(finder, force_root_egg_info=False, bundle=False)
requirement_set.install(install_options, global_options)
print("")
print("Installed")
print("==================================")
[ print(package.name) for package in requirement_set.successfully_installed ]
print("")
@frgomes

This comment has been minimized.

Copy link

frgomes commented Aug 2, 2013

I had troubles with your suggestion. I don't know exactly what the issue was. Then I've decided to find a 'higher level' API which, in theory, should know how to do all the necessary preparation in order to make things work.

You can see my gist at https://gist.github.com/frgomes/6140463

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.