emoji-country-flag

raw JSON →
2.1.0 verified Fri May 01 auth: no python

Encode/decode unicode country flag emoji. Version 2.1.0; supports Python >=3.10. Regularly updated to match ISO 3166-1 changes.

pip install emoji-country-flag
error AttributeError: module 'emoji_country_flag' has no attribute 'get_flag'
cause Importing from the wrong submodule or using incorrect function name; get_flag may not exist in newer versions.
fix
Use from emoji_country_flag import get_flag (if version <1.2.0) or use Flag class.
error ValueError: Invalid country code: us
cause Country code must be uppercase.
fix
Use uppercase letters, e.g., 'US' instead of 'us'.
breaking Version 2.0.0+ requires Python 3.10 or later. Python 3.6-3.9 no longer supported.
fix Upgrade to Python 3.10+ or pin to version 1.x (e.g., pip install 'emoji-country-flag<2')
deprecated The function `get_flag()` is deprecated in favor of `Flag()` class in version 1.2.0+.
fix Use `Flag(code).emoji` instead of `get_flag(code)`.
gotcha Country codes must be ISO 3166-1 alpha-2 uppercase. Lowercase or full country names are not recognized.
fix Always use uppercase two-letter codes (e.g., 'US', 'GB').

Create a Flag object or use functions to encode country code to emoji and decode emoji to country code.

from emoji_country_flag import Flag

flag = Flag(code='US')
print(flag.emoji)  # 🇺🇸

# or using get_flag
from emoji_country_flag import get_flag, decode_flag
emoji = get_flag('US')
print(emoji)  # 🇺🇸
code = decode_flag(emoji)
print(code)  # US