Skip to content

Commit 148610d

Browse files
methaneaeros
andauthored
bpo-39287: Doc: Add UTF-8 mode section in using/windows. (GH-17935)
Co-Authored-By: Kyle Stanley <[email protected]>
1 parent 13c1c35 commit 148610d

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

Doc/using/cmdline.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -929,8 +929,6 @@ conflict.
929929

930930
Also available as the :option:`-X` ``utf8`` option.
931931

932-
.. availability:: \*nix.
933-
934932
.. versionadded:: 3.7
935933
See :pep:`540` for more details.
936934

Doc/using/windows.rst

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,50 @@ existed)::
602602

603603
C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Python 3.9
604604

605+
.. _win-utf8-mode:
606+
607+
UTF-8 mode
608+
==========
609+
610+
.. versionadded:: 3.7
611+
612+
Windows still uses legacy encodings for the system encoding (the ANSI Code
613+
Page). Python uses it for the default encoding of text files (e.g.
614+
:func:`locale.getpreferredencoding`).
615+
616+
This may cause issues because UTF-8 is widely used on the internet
617+
and most Unix systems, including WSL (Windows Subsystem for Linux).
618+
619+
You can use UTF-8 mode to change the default text encoding to UTF-8.
620+
You can enable UTF-8 mode via the ``-X utf8`` command line option, or
621+
the ``PYTHONUTF8=1`` environment variable. See :envvar:`PYTHONUTF8` for
622+
enabling UTF-8 mode, and :ref:`setting-envvars` for how to modify
623+
environment variables.
624+
625+
When UTF-8 mode is enabled:
626+
627+
* :func:`locale.getpreferredencoding` returns ``'UTF-8'`` instead of
628+
the system encoding. This function is used for the default text
629+
encoding in many places, including :func:`open`, :class:`Popen`,
630+
:meth:`Path.read_text`, etc.
631+
* :data:`sys.stdin`, :data:`sys.stdout`, and :data:`sys.stderr`
632+
all use UTF-8 as their text encoding.
633+
* You can still use the system encoding via the "mbcs" codec.
634+
635+
Note that adding ``PYTHONUTF8=1`` to the default environment variables
636+
will affect all Python 3.7+ applications on your system.
637+
If you have any Python 3.7+ applications which rely on the legacy
638+
system encoding, it is recommended to set the environment variable
639+
temporarily or use the ``-X utf8`` command line option.
640+
641+
.. note::
642+
Even when UTF-8 mode is disabled, Python uses UTF-8 by default
643+
on Windows for:
644+
645+
* Console I/O including standard I/O (see :pep:`528` for details).
646+
* The filesystem encoding (see :pep:`529` for details).
647+
648+
605649
.. _launcher:
606650

607651
Python Launcher for Windows

0 commit comments

Comments
 (0)