Changing the “locale preferred encoding” in Python 3 in Windows Changing the “locale preferred encoding” in Python 3 in Windows windows windows

Changing the “locale preferred encoding” in Python 3 in Windows


As of python3.5.1 this hack looks like this:

import _locale_locale._getdefaultlocale = (lambda *args: ['en_US', 'utf8'])

All files opened thereafter will assume the default encoding to be utf8.


i know its a real hacky workaround, but you could redefine the locale.getpreferredencoding() function like so:

import localedef getpreferredencoding(do_setlocale = True):    return "utf-8"locale.getpreferredencoding = getpreferredencoding

if you run this early on, all files opened after (at lest in my testing on a win xp machine) open in utf-8, and as this overrides the module method this would apply to all platforms.


The post is old but the issue is still of actuality (under Python 3.7 and Windows 10).

I've improved the solution as follows, making sure that the language/country part isn't overwritten but only the encoding, and also to make sure that it is only done under Windows:

if os.name == "nt":    import _locale    _locale._gdl_bak = _locale._getdefaultlocale    _locale._getdefaultlocale = (lambda *args: (_locale._gdl_bak()[0], 'utf8'))

Hope this helps...