Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script for an Alfred shortcut to open PyPI docs
#!/usr/bin/env python
# -*- encoding: utf-8
"""
Try to open the documentation for a Python package, or the PyPI page if
it can't be found.
"""
import json
import sys
import webbrowser
try:
from urllib.request import urlopen
except ImportError: # Python 2
from urllib2 import urlopen
package_name = sys.argv[1]
resp = urlopen("https://pypi.org/pypi/%s/json" % package_name)
data = json.load(resp)
try:
docs_url = data["info"]["project_urls"]["Documentation"]
except KeyError:
docs_url = "https://pypi.org/project/%s/" % package_name
webbrowser.open(docs_url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment