Countryinfo Python Library
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
- gotcha Initializing `CountryInfo` with a non-existent country name will raise a `CountryNotFoundError`.
- gotcha Passing `None` or an empty string to the `CountryInfo` constructor will raise a `ValueError`.
- gotcha Region names (e.g., 'Africa', 'Europe') are not valid inputs for the `CountryInfo` constructor. It expects specific country identifiers.
- gotcha Users have reported `KeyError` or issues with data loading, sometimes indicating missing data files or corrupted installations.
- gotcha Environments with non-UTF-8 default encodings may encounter `UnicodeDecodeError` or `charmap` errors when processing certain country names or data.
Install
-
pip install countryinfo -
pip install "countryinfo[fuzzy]" -
pip install "countryinfo[pydantic]" -
pip install "countryinfo[all]"
Imports
- CountryInfo
from countryinfo import CountryInfo
- CountryNotFoundError
from countryinfo import CountryInfo, CountryNotFoundError
Quickstart
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])}...")