Skip to content

Instantly share code, notes, and snippets.

@alexwlchan
Last active September 22, 2019 17:06
Show Gist options
  • Save alexwlchan/e28ec1ee5ac16de450cf06664e9e8471 to your computer and use it in GitHub Desktop.
Save alexwlchan/e28ec1ee5ac16de450cf06664e9e8471 to your computer and use it in GitHub Desktop.
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