Global unicode setting for Py2Exe

Dear List,
In a current project i use minidom to import and parse a project XML file
and fill up the user interface with these values. For my python installation
(Python 2.3.3, wxPython 2.5.1.5) I use a sitecustomize.py file to set the
internal string codec globally to "iso-8859-1". The XML parser in the
distributed project (generated with py2exe) fails with the well known error
"ordinal not in range (128)" etc.
I was looking around the net and found many threads about this issue, but
i'm still uncomfortable with the answers. So there's just a very simple
question and maybe somebody has got the answer:
Can I customize the unicode codec in the standalone application generated
with py2exe or ain't that possible, even not in a future release of py2exe?
In the setup.py i already defined
options={"py2exe":{"packages":["encodings"]...
and all string codecs are well contained in the project. If the global
unicode setting cannot be customized there is only the call to encode()
decode() to those strings, am i right? This would cause a lot of work,
though...
Best regards

Oliver Walczak

Oliver Walczak wrote:

Dear List,
In a current project i use minidom to import and parse a project XML file
and fill up the user interface with these values. For my python installation
(Python 2.3.3, wxPython 2.5.1.5) I use a sitecustomize.py file to set the
internal string codec globally to "iso-8859-1". The XML parser in the
distributed project (generated with py2exe) fails with the well known error
"ordinal not in range (128)" etc.
I was looking around the net and found many threads about this issue, but
i'm still uncomfortable with the answers. So there's just a very simple
question and maybe somebody has got the answer:
Can I customize the unicode codec in the standalone application generated
with py2exe or ain't that possible, even not in a future release of py2exe?
In the setup.py i already defined
options={"py2exe":{"packages":["encodings"]...
and all string codecs are well contained in the project. If the global
unicode setting cannot be customized there is only the call to encode()
decode() to those strings, am i right? This would cause a lot of work,
though...

Why not move the code you put in sitecustomize.py into the startup code of your app?

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Oliver Walczak wrote:

I use a sitecustomize.py file to set the
internal string codec globally to "iso-8859-1". The XML parser in the
distributed project (generated with py2exe) fails with the well known error
"ordinal not in range (128)" etc.

I use this hack (courtesy of Oleg Broytmann):

reload(sys)
sys.setdefaultencoding(codepage)

at the start of my application.

···

--
Oleg Deribas,
http://copi.ru/14000/

Oleg Deribas <rus43@kharkov.ukrtel.net> writes:

Oliver Walczak wrote:

I use a sitecustomize.py file to set the
internal string codec globally to "iso-8859-1". The XML parser in the
distributed project (generated with py2exe) fails with the well known error
"ordinal not in range (128)" etc.

I use this hack (courtesy of Oleg Broytmann):

reload(sys)
sys.setdefaultencoding(codepage)

at the start of my application.

I do not know whether it is dangerous to replace the default encoding
afterwards or not. site.py which is normally run before the main
script, removes the setdefaultencoding method from the sys module.

The py2exe'd script doesn't run the code in site.py (same effect can be
achived in Python with the -S command line flag), so the
setdefaultencoding method is still present.

And I had no problems freezing a script and running the exe afterwards
by inserting this code at the top:

import sys
if hasattr(sys, "frozen") is not None:
    sys.setdefaultencoding("iso-8859-1")
    print "äöü".encode()

from wxPython.wx import *

class MyFrame(wxFrame):
    ....

The print "äöü".encode() statement shows that it works, I only had to
give the '-p encodings' command line flag to py2exe. This was with the
advanced sample provided with py2exe.

Thomas