Registry / data / pycountry

pycountry

JSON →
library26.2.16pypypi✓ verified 27d ago

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.

pip install pycountry
INSTALL
IMPORT
SIG · PYCOUNTRY
P
pycountry
datapythonv26.2.16
Install
1.9s avg
Import
126ms
Disk
38MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v26.2.16 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.132s · 40.5MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.9s · import 0.120s · 41MB
38MB installed
● package 38MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

countries
import pycountry country = pycountry.countries.get(alpha_2='US')
Provides access to ISO 3166-1 country data.
subdivisions
import pycountry subdivision = pycountry.subdivisions.get(code='US-CA')
Provides access to ISO 3166-2 country subdivision data.
languages
import pycountry language = pycountry.languages.get(alpha_3='eng')
Provides access to ISO 639-3 language data.
currencies
import pycountry currency = pycountry.currencies.get(alpha_3='USD')
Provides access to ISO 4217 currency data.
scripts
import pycountry script = pycountry.scripts.get(alpha_4='Latn')
Provides access to ISO 15924 script data.
historic_countries
import pycountry historic_country = pycountry.historic_countries.get(alpha_3='SUN')
Provides access to ISO 3166-3 historic/deleted country data.
lookup
import pycountry country = pycountry.countries.lookup('United States')
country = pycountry.countries.get(name='United States')
'.get()' requires an exact match on a specific attribute. '.lookup()' provides a broader, more forgiving search across multiple attributes (name, common_name, official_name, codes).

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.")
Debug
Known issues
breakingIn version 24.6.1, a fallback mechanism was removed. Previously, if `country.common_name` or `country.official_name` were unavailable, it would fall back to `country.name`. This fallback was reverted. Code relying on this implicit fallback will now raise `AttributeError` if the specific attribute is missing.
fix
Always check for attribute existence using `getattr(obj, 'attribute_name', default_value)` or handle `AttributeError` explicitly. Prefer `pycountry.countries.lookup()` for flexible name-based searches, which returns the most relevant match.
affects: >=24.6.1
gotchaThe data in pycountry is a direct wrapper around Debian's `pkg-isocodes` database. This means `pycountry` itself does not accept direct contributions for changes to country, language, or currency data. Any required changes to the underlying ISO data must be addressed with ISO or Debian's `pkg-isocodes` project.
fix
Understand that `pycountry` is a data consumer, not a data source for ISO standards. For political or data accuracy issues, upstream sources (ISO, Debian) should be consulted.
affects: All versions
deprecatedSupport for Python 3.6 and 3.7 was officially dropped in version 23.12.11.
fix
Ensure your project runs on Python 3.8 or newer. The current `pycountry` version requires Python >=3.10.
affects: >=23.12.11
gotchaWhen retrieving objects, using `.get()` requires an exact match on a specified attribute (e.g., `alpha_2`, `name`). If an attribute like `official_name` or `common_name` might not always exist for all entries, direct access (e.g., `country.official_name`) can raise an `AttributeError`.
fix
Use `getattr(obj, 'attribute_name', default_value)` to safely access potentially missing attributes, or implement `try-except AttributeError`. For searching, use `pycountry.collection.lookup('search_string')` as it provides a robust search across various naming attributes and handles unicode normalization.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pycountry'
The pycountry library has not been installed in the Python environment being used, or the environment where it's installed is not the one running the script.
fix
Ensure pycountry is installed using pip: `pip install pycountry`. If using a virtual environment, activate it before installing.
KeyError: 'XX' (where 'XX' is a country code or name)
This error occurs when attempting to retrieve a country, language, or other ISO standard entry using a key (like alpha_2, alpha_3, or name) that does not exist in the pycountry database for the given value, or when using an outdated key name (e.g., 'alpha2' instead of 'alpha_2').
fix
Use the `get()` method with a default fallback (e.g., `pycountry.countries.get(alpha_2='XX', default=None)`) or wrap the lookup in a `try-except KeyError` block. Verify that the key name is correct (e.g., `alpha_2` for ISO 3166-1 alpha-2 codes).
AttributeError: 'Country' object has no attribute 'common_name'
This typically happens when trying to access an attribute (like 'common_name' or 'official_name') that might not exist for every single entry in the pycountry databases, or when using an outdated attribute name (e.g., 'alpha3' instead of 'alpha_3').
fix
Use `getattr()` with a default value (e.g., `getattr(country, 'common_name', country.name)`) or check for the attribute's existence using `hasattr(country, 'common_name')` before accessing it. Ensure you are using the correct, up-to-date attribute names (e.g., 'alpha_3' for alpha-3 codes).
pkg_resource.DistributionNotFound: The 'pycountry' distribution was not found and is required by the application.
When packaging a Python application with tools like PyInstaller or cx_Freeze, the metadata for `pycountry` is not automatically collected and bundled, leading to this error when the compiled executable tries to run.
fix
For PyInstaller, you need to explicitly include `pycountry`'s metadata in the `.spec` file. Add `from PyInstaller.utils.hooks import copy_metadata` at the top and modify the `datas` array in the `Analysis` section to include `datas=copy_metadata('pycountry'),`.
Upgrade
Version history
26.2.16latest on PyPI · released Feb 17, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
8
Amazon
1
Resources
pycountry — pip install pycountry · libregistry