Countryinfo Python Library

1.0.1 · active · verified Sun Apr 12

A lightweight Python library for accessing comprehensive country data — including ISO codes, states/provinces, capital cities, currencies, languages, and other geographic information. The current version is 1.0.1. The project maintains an active development status, with the latest release in March 2026.

Warnings

Install

Imports

Quickstart

Initializes `CountryInfo` with a country name (or ISO code/numeric ID) and demonstrates fetching its capital and ISO code. It also includes error handling for non-existent countries and demonstrates filtering all available countries by region.

from countryinfo import CountryInfo, CountryNotFoundError
import os

try:
    # Use a well-known country for the quickstart
    country = CountryInfo("Singapore")
    print(f"Capital of Singapore: {country.capital()}")

    # Example of retrieving ISO information
    print(f"ISO Alpha-2 code: {country.iso(2)}")

    # Example with a non-existent country to demonstrate error handling
    non_existent_country = "Neverland"
    print(f"\nTrying to get info for '{non_existent_country}':")
    CountryInfo(non_existent_country).name()
except CountryNotFoundError as e:
    print(f"Error: {e}")
except ValueError as e:
    print(f"Error: {e}")

# To demonstrate filtering countries by region
print("\nFetching some countries in Europe:")
europe_countries = [c['name'] for c in CountryInfo.all().values() if c.get('region') == 'Europe']
if europe_countries:
    print(f"Example European countries: {', '.join(europe_countries[:5])}...")

view raw JSON →