win-unicode-console
win-unicode-console is a Python library (current version 0.5) designed to enable proper Unicode input and output within the Windows console for Python versions up to 3.5. It addresses historical encoding issues when running Python scripts directly in the Windows console, allowing for correct display and handling of non-ASCII characters. The library is considered to be in maintenance mode, as Python 3.6 and newer versions have resolved the underlying console encoding issues via PEP 528.
Common errors
-
UnicodeEncodeError: 'charmap' codec can't encode character '\uXXXX' in position Y: character maps to <undefined>
cause The default Windows console encoding (e.g., CP437 or CP1252) cannot represent the Unicode character you are trying to print. This is a common issue for Python versions prior to 3.6 on Windows.fixInstall and call `win_unicode_console.enable()` at the beginning of your script. Additionally, ensure your console font supports the required Unicode characters. -
Unicode characters display as '?' or squares in the Windows console output.
cause This typically indicates either that `win-unicode-console` is not active, or the selected console font does not support the specific Unicode characters being displayed.fixMake sure `win_unicode_console.enable()` is called. Then, change your console font to 'Lucida Console' or 'Consolas' through the console window's properties to ensure full Unicode glyph support. -
Scripts launched with `python -m run script.py` (win-unicode-console runner) fail or behave unexpectedly with custom readline hooks or Python 2 `raw_input`.
cause Prior to version 0.4, `win-unicode-console` used a custom REPL. Changes in 0.4 and later versions refined how the runner works and deprecated certain direct manipulations of `raw_input`. Python 2's `raw_input` returns bytes, which needs careful handling when Unicode is expected.fixReview the `win-unicode-console` README for specific `runner` usage (e.g., `py -i -m run` for interactive mode). For Python 2, if `raw_input` issues persist with Unicode, you might need `win_unicode_console.enable(raw_input__return_unicode=False)` if your IPython version is older than 4.
Warnings
- breaking This package is not necessary for Python 3.6 and newer versions because PEP 528 introduced native UTF-8 support for the Windows console, resolving the underlying issues that `win-unicode-console` addresses. Using it with Python 3.6+ is generally harmless but redundant.
- gotcha Even with `win-unicode-console`, the Windows console must be configured to use a font that supports Unicode characters (e.g., 'Lucida Console' or 'Consolas') to display them correctly. Without a suitable font, characters may still appear as squares or question marks.
- breaking Version 0.4 changed the signature of `streams.enable` and removed `streams.enable_reader`, `streams.enable_writer`, and `streams.enable_error_writer`. Code using these older stream-specific functions will break.
- gotcha Manually setting the console code page to `chcp 65001` (UTF-8) is often problematic on Windows. It can cause issues with `sys.stdin`, result in `IOError`s, or not fully resolve Unicode display due to limitations of the standard console APIs. `win-unicode-console` is designed to work around these limitations without requiring `chcp 65001`.
Install
-
pip install win-unicode-console
Imports
- enable
import win_unicode_console win_unicode_console.enable()
Quickstart
import win_unicode_console
# Enable Unicode console support (should be called early)
win_unicode_console.enable()
# Now, print Unicode characters
print('こんにちは世界 (Hello world in Japanese)')
print('Français: bonjour, ça va? (French)')
# Try reading Unicode input (on Python 2, this might still need custom readline hook or specific configurations)
# For simple testing, focus on output initially.
# For Python 3, input() typically handles Unicode after enable().
# Example for Python 3:
try:
user_input = input('Enter some Unicode text: ')
print(f'You entered: {user_input}')
except Exception as e:
print(f'Input error: {e}')