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.
pip install countryinfoVerified import paths — ran on the pinned version, not inferred.
Initializes `CountryInfo` with a country name (or ISO code/numeric ID) and demonstrates fetching its capital and ISO code. It also includes error handling for non-existent countries and demonstrates filtering all available countries by region.
Wrap the `CountryInfo` constructor in a `try-except CountryNotFoundError` block.
Ensure the input to `CountryInfo` is a non-empty string or a valid integer (for ISO numeric).
To get countries within a region, use `CountryInfo.all()` to retrieve all country data and then filter the results based on the 'region' key.
Verify the `countryinfo/data` directory exists within your `site-packages` and contains the necessary JSON files. Reinstalling the package might resolve the issue.
Ensure your Python environment is configured to use UTF-8 encoding. This can often be set via environment variables (e.g., `PYTHONIOENCODING=UTF-8`) or by running Python with the `-X utf8` flag.
Install the library using pip: `pip install countryinfo`
Ensure the country name or ISO code is valid and correctly spelled. Wrap the constructor call in a `try-except CountryNotFoundError` block for robust error handling. Example: `from countryinfo import CountryInfo, CountryNotFoundError
try:
country = CountryInfo('Singapore')
except CountryNotFoundError as e:
print(f'Error: {e}')`Provide a valid, non-empty string (country name or ISO code) or an integer (ISO numeric code) to the `CountryInfo` constructor. Example: `country = CountryInfo('USA')` or `country = CountryInfo(840)`Verify the exact keys for accessing country properties (e.g., `country.capital()` instead of `country.capital`). If the error persists after checking keys, reinstall the `countryinfo` package to ensure data files are intact: `pip install --upgrade --force-reinstall countryinfo`.
Ensure your Python environment is configured to use UTF-8 encoding. You can set the `PYTHONIOENCODING` environment variable to `UTF-8` before running your script, or open the relevant file with `encoding='utf-8'` if manually loading data (though this fix might require modifying the library's source for deeply embedded file operations). Example of setting environment variable (in terminal before running python script): `set PYTHONIOENCODING=UTF-8` (Windows) or `export PYTHONIOENCODING=UTF-8` (Linux/macOS).