Skip to content

Instantly share code, notes, and snippets.

@d0c-s4vage
Created March 13, 2016 13:32
Show Gist options
  • Save d0c-s4vage/14a34f3ad815906097ad to your computer and use it in GitHub Desktop.
Save d0c-s4vage/14a34f3ad815906097ad to your computer and use it in GitHub Desktop.
A simple script to auto-install packages from pypi when they are imported.
#!/usr/bin/env python
# encoding: utf-8
import imp
import os
import pdb
import pip
import readline
from pip.commands.search import SearchCommand
import sys
import virtualenv
class PyPiPathHook(object):
def __init__(self):
# create a virtualenv at ~/.pypi_autoload
user_home = os.path.expanduser("~")
self.venv_home = os.path.join(user_home, ".pypi_autoload")
if not os.path.exists(self.venv_home):
virtualenv.create_environment(self.venv_home)
activate_script = os.path.join(self.venv_home, "bin", "activate_this.py")
execfile(activate_script, dict(__file__=activate_script))
def find_module(self, fullname, path=None):
if "." in fullname:
return None
try:
mod = imp.find_module(fullname)
except ImportError as e:
pass
else:
# it's already accessible, we don't need to do anything
return None
if self._package_exists_in_pypi(fullname):
pip.main(["install", fullname, "--prefix", self.venv_home])
# we've made it accessible to the normal import procedures
# now, (should be on sys.path), so we'll return None which
# will make Python attempt a normal import
return None
def _package_exists_in_pypi(self, fullname):
searcher = SearchCommand()
options,args = searcher.parse_args([fullname])
matches = searcher.search(args, options)
found_match = None
for match in matches:
if match["name"] == fullname:
return True
break
return False
sys.meta_path.append(PyPiPathHook())
@d0c-s4vage
Copy link
Author

Usage:

import autopypi
import xmltodict # will be auto-installed to a virtualenv at ~/.pypi_autoload if not already installed

# profit!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment