Skip to content

Instantly share code, notes, and snippets.

@rwilcox
Created December 26, 2010 17:32
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwilcox/755524 to your computer and use it in GitHub Desktop.
Save rwilcox/755524 to your computer and use it in GitHub Desktop.
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
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