`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.
pip install iso3166Verified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to import the `countries` object and retrieve country data using various ISO 3166-1 codes (alpha-2, alpha-3, numeric) or iterate through the entire list. Each country object provides `name`, `alpha2`, `alpha3`, and `numeric` attributes.
If you need subdivision data, install `iso3166-2` (`pip install iso3166-2`). If you only need top-level country codes, `iso3166` is correct.
Try updating the package (`pip install --upgrade iso3166`), uninstalling and reinstalling it, or restarting your Python kernel/environment. Manual deletion of residual package files might also be necessary in some cases.
Be aware that major new features or significant code changes are less likely from the original maintainer. The library remains suitable for its core purpose of ISO 3166-1 data lookup.
Install the library using pip: `pip install iso3166`. If using an IDE like PyCharm, ensure the correct Python interpreter where the package is installed is selected for the project.
Try reinstalling the package: `pip uninstall iso3166 && pip install iso3166`. If in an IDE, restarting the IDE or clearing its cache might help. Verify the import statement `from iso3166 import countries` is correct according to the documentation.
Ensure the country code or name is valid according to the ISO 3166-1 standard. To handle missing entries gracefully, use the `.get()` method with a default value, for example: `country = countries.get('XX', None)` or `country = countries.get('XX', 'Unknown Country')`.Always check if the result from `countries.get()` is not `None` before attempting to access its attributes. For example:
```python
from iso3166 import countries
country_code = 'ZZ'
country = countries.get(country_code)
if country:
print(country.name)
else:
print(f'Country with code {country_code} not found.')
```No dependency data recorded yet.