pycountry

26.2.16 · active · verified Sun Mar 29

pycountry is a Python library that provides access to various ISO standards databases, including ISO 3166 (countries and their subdivisions), ISO 639 (languages), ISO 4217 (currencies), and ISO 15924 (scripts). It acts as a pure wrapper around Debian's `pkg-isocodes` database, making this data accessible through a Python API, complete with translation files. The library is actively maintained, with data updates typically following the `pkg-isocodes` updates rather than a fixed release schedule. The current version is 26.2.16.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to retrieve country and language information using specific ISO codes and how to use the more flexible `lookup()` method for searching across different name attributes.

import pycountry

# Get a country by its Alpha-2 code
try:
    usa = pycountry.countries.get(alpha_2='US')
    print(f"Country Name: {usa.name}, Alpha-3: {usa.alpha_3}")
except KeyError:
    print("Country not found.")

# Get a language by its Alpha-3 code
try:
    english = pycountry.languages.get(alpha_3='eng')
    print(f"Language Name: {english.name}")
except KeyError:
    print("Language not found.")

# Search for a country using a more flexible lookup
try:
    bolivia_lookup = pycountry.countries.lookup('Bolivia')
    print(f"Lookup for Bolivia: {bolivia_lookup.name}, Official: {getattr(bolivia_lookup, 'official_name', 'N/A')}")
except LookupError:
    print("Bolivia not found via lookup.")

view raw JSON →