Search code examples
pythonwindowsencodingwindows-xpcx-freeze

Python 3.4.4/win xp: encoding error


I have a python script written (and fully working) on debian. Now I would like to freeze the script with cx_Freeze for distribution to windows users and for that purpose I have a win xp system running in virtualbox with python 3.4.4 installed on it. However, after freezing the script and an attempt to run the resulting exe, I get this:

"UnicodeEncodeError: 'charmap' codec can't encode characters in position 11-14: character maps to (<)undefined(>)".

The characters in issue are these:

ту╧╙

The characters are a part of syntax of an fdf file that the python script generates. It also came out that even the original python script before freezing has the same issue; I understand python's stdout is cp852 while the system's command line interpreter operates with cp1250. I have been trying various ways to change PYTHONIOENCODING but to no avail so far, it only leads to even more error messages. Is there something I could do to fix this?


Solution

  • Are you setting the encoding of the file you generate? If not, it will default to the system encoding and that could explain your problem. So you can open the output file like this (substitute utf8 by what you actually need):

    stream = open('filename', 'w', encoding='utf8')
    

    Or maybe you should actually open your output file in binary mode. It seems odd that a string of unicode characters varying between Cyrillic and box drawing would actually be the syntax for something - probably what you need is the byte value of those characters in a specific 8-bit encoding.