{"id":4483,"library":"countryinfo","title":"Countryinfo Python Library","description":"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.","status":"active","version":"1.0.1","language":"en","source_language":"en","source_url":"https://github.com/porimol/countryinfo","tags":["country data","ISO codes","geography","utility","country information"],"install":[{"cmd":"pip install countryinfo","lang":"bash","label":"Standard installation"},{"cmd":"pip install \"countryinfo[fuzzy]\"","lang":"bash","label":"With fuzzy matching for typo tolerance"},{"cmd":"pip install \"countryinfo[pydantic]\"","lang":"bash","label":"With Pydantic v2 support for typed models"},{"cmd":"pip install \"countryinfo[all]\"","lang":"bash","label":"All optional extras"}],"dependencies":[{"reason":"For fuzzy/typo-tolerant country lookup (optional)","package":"fuzzy","optional":true},{"reason":"For Pydantic v2 typed models (optional)","package":"pydantic","optional":true}],"imports":[{"symbol":"CountryInfo","correct":"from countryinfo import CountryInfo"},{"note":"Import CountryNotFoundError for explicit error handling.","symbol":"CountryNotFoundError","correct":"from countryinfo import CountryInfo, CountryNotFoundError"}],"quickstart":{"code":"from countryinfo import CountryInfo, CountryNotFoundError\nimport os\n\ntry:\n    # Use a well-known country for the quickstart\n    country = CountryInfo(\"Singapore\")\n    print(f\"Capital of Singapore: {country.capital()}\")\n\n    # Example of retrieving ISO information\n    print(f\"ISO Alpha-2 code: {country.iso(2)}\")\n\n    # Example with a non-existent country to demonstrate error handling\n    non_existent_country = \"Neverland\"\n    print(f\"\\nTrying to get info for '{non_existent_country}':\")\n    CountryInfo(non_existent_country).name()\nexcept CountryNotFoundError as e:\n    print(f\"Error: {e}\")\nexcept ValueError as e:\n    print(f\"Error: {e}\")\n\n# To demonstrate filtering countries by region\nprint(\"\\nFetching some countries in Europe:\")\neurope_countries = [c['name'] for c in CountryInfo.all().values() if c.get('region') == 'Europe']\nif europe_countries:\n    print(f\"Example European countries: {', '.join(europe_countries[:5])}...\")\n","lang":"python","description":"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."},"warnings":[{"fix":"Wrap the `CountryInfo` constructor in a `try-except CountryNotFoundError` block.","message":"Initializing `CountryInfo` with a non-existent country name will raise a `CountryNotFoundError`.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure the input to `CountryInfo` is a non-empty string or a valid integer (for ISO numeric).","message":"Passing `None` or an empty string to the `CountryInfo` constructor will raise a `ValueError`.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"To get countries within a region, use `CountryInfo.all()` to retrieve all country data and then filter the results based on the 'region' key.","message":"Region names (e.g., 'Africa', 'Europe') are not valid inputs for the `CountryInfo` constructor. It expects specific country identifiers.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Verify the `countryinfo/data` directory exists within your `site-packages` and contains the necessary JSON files. Reinstalling the package might resolve the issue.","message":"Users have reported `KeyError` or issues with data loading, sometimes indicating missing data files or corrupted installations.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"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.","message":"Environments with non-UTF-8 default encodings may encounter `UnicodeDecodeError` or `charmap` errors when processing certain country names or data.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}