DEV Community

Inada Naoki
Inada Naoki

Posted on • Edited on

22 1 1

Python: Use the UTF-8 mode on Windows!

Summary: Set the PYTHONUTF8=1 environment variable.

On macOS and Linux, UTF-8 is the standard encoding already.
But Windows still uses legacy encoding (e.g. cp1252, cp932, etc...) as system encoding.

Python works very well about file names and console IO (e.g. use ~W APIs). But the legacy system encoding is used for the default encoding of text files and pipes.

It is a very common mistake that omits the encoding="utf-8" option. Developers who use macOS or Linux doesn't have any trouble by the mistake.

For example, even the packaging tutorial in the packaging.python.org uses this code snippet:

with open("README.md", "r") as fh:
    long_description = fh.read()

When README.md contains some non-ASCII characters (e.g. Unicode emoji), setup.py will fail on Windows. Windows users can not install the package when wheel is not provided. (I sent a pull request to fix this example code already.)

I believe many Python programmers are suffered by this default text file encoding on Windows because:

  • The default encoding of Python source code is UTF-8
  • UTF-8 is the standard encoding of the Web
  • Modern text editors like VS Code use UTF-8 by default. And even the notepad.exe chose UTF-8 for the default encoding!

But it is difficult to change the default encoding of text files because it is backward incompatible change. It will break some legacy applications which relying on the legacy encoding.

But there is good news: Python 3.7 introduced the "UTF-8 mode". (thanks to Victor Stinner!!)

When UTF-8 mode is enabled, Python uses UTF-8 as default encoding for text files instead of system encoding.
You can live in the world "UTF-8 is the default. Other legacy encodings are used only when explicitly specified." like macOS and Linux.

To enable UTF-8 mode:

  • Set the the environment variable PYTHONUTF8=1, or
  • Use -Xutf8 command line option.

Playwright CLI Flags Tutorial

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • 0:56 --last-failed
  • 2:34 --only-changed
  • 4:27 --repeat-each
  • 5:15 --forbid-only
  • 5:51 --ui --headed --workers 1

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Click on any timestamp above to jump directly to that section in the tutorial!

Watch Full Video 📹️

Top comments (3)

Collapse
 
aliwo profile image
triumph1

You saved millions of windows users (including me)! Thank you :)

Collapse
 
krave1986 profile image
krave1986

Many Thanks!

Collapse
 
kreas profile image
Tim Warren

Thank you... But why was this ever a thing?!

Playwright CLI Flags Tutorial

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • 0:56 --last-failed: Zero in on just the tests that failed in your previous run
  • 2:34 --only-changed: Test only the spec files you've modified in git
  • 4:27 --repeat-each: Run tests multiple times to catch flaky behavior before it reaches production
  • 5:15 --forbid-only: Prevent accidental test.only commits from breaking your CI pipeline
  • 5:51 --ui --headed --workers 1: Debug visually with browser windows and sequential test execution

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Click on any timestamp above to jump directly to that section in the tutorial!

Watch Full Video 📹️

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay