PyZipCode

3.0.1 · active · verified Thu Apr 16

PyZipCode is a Python library designed to query zip code and location data. It uses a local SQLite database, typically derived from sources like Maxmind Cities DB or older Census Bureau data, to provide information such as city, state, latitude, longitude, timezone, and daylight savings time flag for US zip codes. The current stable version is 3.0.1, last released in March 2021, and it supports Python 3.6+. Releases are infrequent but address compatibility and bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

Initializes the ZipCodeDatabase and demonstrates basic lookups by zip code, searching by city, and finding zip codes within a geographical radius.

from pyzipcode import ZipCodeDatabase

zcdb = ZipCodeDatabase()

# Lookup a specific zip code
try:
    zipcode_entry = zcdb['10013']
    print(f"Zip: {zipcode_entry.zip}, City: {zipcode_entry.city}, State: {zipcode_entry.state}, Lat: {zipcode_entry.latitude}, Lng: {zipcode_entry.longitude}")
except KeyError:
    print("Zip code not found.")

# Find zip codes by city
manhattan_zips = zcdb.find_zip(city='Manhattan')
print(f"\nZip codes in Manhattan: {[z.zip for z in manhattan_zips[:5]]}...")

# Get zip codes around a radius
zipcodes_in_radius = zcdb.get_zipcodes_around_radius('90210', 10)
print(f"\nZip codes within 10 miles of 90210: {[z.zip for z in zipcodes_in_radius[:5]]}...")

view raw JSON →