Last modified: Jun 09, 2025 By Alexander Williams
Install pycountry in Python - Quick Guide
pycountry is a Python library for accessing ISO country, language, and currency data. It simplifies working with standardized codes.
What is pycountry?
pycountry provides easy access to ISO databases. It includes country, language, and currency information. The library is useful for data validation and internationalization tasks.
Installation Methods
You can install pycountry using pip. It requires Python 3.6 or higher.
Install via pip
The simplest way to install pycountry is using pip:
# Install pycountry using pip
pip install pycountry
Successfully installed pycountry-22.3.5
Verify Installation
Check if pycountry installed correctly:
# Verify installation
import pycountry
print(pycountry.__version__)
22.3.5
Basic Usage Examples
pycountry provides three main datasets: countries, languages, and currencies.
Working with Countries
Get country information using ISO codes:
# Get country by alpha-2 code
us = pycountry.countries.get(alpha_2='US')
print(us.name)
United States
Working with Languages
Access language data using ISO 639 codes:
# Get language by code
eng = pycountry.languages.get(alpha_2='en')
print(eng.name)
English
Working with Currencies
Retrieve currency information:
# Get currency by code
usd = pycountry.currencies.get(alpha_3='USD')
print(usd.name)
US Dollar
Advanced Features
pycountry offers more than basic lookups. You can search and filter data.
Searching Countries
Find countries by name or other attributes:
# Search countries by name
for country in pycountry.countries.search_fuzzy('Britain'):
print(country.name)
United Kingdom
Historical Countries
Access historical country data:
# Check for historical countries
ussr = pycountry.historic_countries.get(alpha_3='SUN')
print(ussr.name if ussr else "Not found")
USSR, Union of Soviet Socialist Republics
Common Use Cases
pycountry is useful in many scenarios. Here are some practical applications.
Form Validation
Validate country or language inputs in forms:
# Validate country code
def is_valid_country(code):
return pycountry.countries.get(alpha_2=code) is not None
print(is_valid_country('XX')) # False
print(is_valid_country('US')) # True
False
True
Data Normalization
Standardize country names in datasets:
# Normalize country names
def normalize_country(name):
try:
return pycountry.countries.search_fuzzy(name)[0].name
except LookupError:
return name
print(normalize_country('america'))
United States
Troubleshooting
Here are solutions to common issues with pycountry.
Missing Data
Some codes might be missing. Check the ISO standards for updates.
Performance Considerations
For large datasets, consider caching results. Repeated lookups can be slow.
Conclusion
pycountry is a powerful tool for working with ISO country, language, and currency data. It's easy to install and use in Python projects.
For similar geospatial libraries, check our guides on GeoPandas and Cartopy. For data handling, see our Python-docx guide.
The library simplifies internationalization tasks and data validation. It's a valuable addition to any Python developer's toolkit.