I have problem with Unicode strings in frozen app. I use python 3.4.1 32bit (on Windows 7 64bit Pro) and py2exe-3 from svn repository. I can demonstrate it with following code:
# -*- coding: utf-8 -*-
# file: test_py2exe.py
import sys
my_string = u"""This is a test:
ábc
End of test..."""
filename = 'test.txt'
if getattr(sys, 'frozen', False):
filename = 'test-frozen.txt'
f = open(filename, mode='w', encoding='utf-8')
f.write(my_string)
f.close()
If I run in standard python shell (py test_py2exe.py) the second line in test.txt is like this (correct):
ábc
If I create frozen app with
py -3.4 -m py2exe.build_exe test_py2exe.py
and run 'dist\test_py2exe.exe' I have in test-frozen.txt second line like this:
ábc
This problem is not related to storing strings to file only, but also when I use other modules (e.g. PyQt5, xlsxwriter) with unicode strings.
I found workaround for that but it is not convenient:
sys_enc = sys.getdefaultencoding() # 'utf-8'
locale_enc = locale.getpreferredencoding() # 'cp1250'
my_string.encode('cp1250').decode('utf-8')
I think I found the problem.
Can you please try if following patch works for you:
Last edit: Thomas Heller 2014-08-11
Thanks. This change fixed my problem.