ISO 3166-1 Country Definitions
`iso3166` is a lightweight Python library providing self-contained definitions for ISO 3166-1 country codes. It allows conversion between two-letter (alpha-2), three-letter (alpha-3), three-digit (numeric) codes, and country names. The current version is 2.1.1, released in July 2022, and updates are typically made to incorporate changes in the ISO 3166-1 standard.
Warnings
- gotcha This library (`iso3166`) *only* provides ISO 3166-1 country codes (e.g., 'US', 'USA'). It does not include ISO 3166-2 country subdivisions (e.g., states, provinces, regions). For subdivision data, consider libraries like `iso3166-2` which is a separate package.
- gotcha Users sometimes encounter `ImportError: cannot import name 'countries' from 'iso3166'`. This can be due to environment issues, incorrect package installation, or cached interpreter states (e.g., in Jupyter notebooks).
- gotcha The official GitHub repository (`deactivated/python-iso3166`) is marked as 'deactivated'. While the PyPI package still receives updates (primarily for ISO data changes) and is functional, the repository status might suggest less active new feature development or community interaction compared to other actively maintained projects.
Install
-
pip install iso3166
Imports
- countries
from iso3166 import countries
Quickstart
from iso3166 import countries
# Get a country by its alpha-2 code
us = countries.get('US')
print(f"Alpha-2: {us.alpha2}, Name: {us.name}, Alpha-3: {us.alpha3}, Numeric: {us.numeric}")
# Get a country by its alpha-3 code
japan = countries.get('JPN')
print(f"Alpha-2: {japan.alpha2}, Name: {japan.name}")
# Iterate through all countries
for country in countries:
if country.numeric == '036': # Australia
print(f"Found country by numeric code: {country.name}")
break