Ben Edwards (lists) wrote: > I am using python 2.4 on Ubuntu dapper, I am working through Dive into > Python. > > There are a couple of inconsictencies. > > Firstly sys.setdefaultencoding('iso-8859-1') does not work, I have to do > sys.setdefaultencoding = 'iso-8859-1'
When you run a Python script, the interpreter does some of its own stuff before executing your script. One of the things it does is to delete the name sys.setdefaultencoding. This means that by the time even your first line of code runs that name no longer exists and so you will be unable to invoke the function as in your first attempt. The second attempt "sys.setdefaultencoding = 'iso-8859-1' " is creating a new name under the sys namespace and assigning it a string. This will not have the desired effect, or probably any effect at all. I have found that in order to change the default encoding with that function, you can put the command in a file called sitecustomize.py which, when placed in the appropriate location (which is platform-dependent), will be called in time to have the desired effect. So the order of events is something like: 1. Invoke Python on myscript.py 2. Python does some stuff and then executes sitecustomize.py 3. Python deletes the name sys.setdefaultencoding, thereby making the function that was so-named inaccessible. 4. Python then begins executing myscript.py. Regarding the location of sitecustomize.py, on Windows it is C:\Python24\Lib\sitecustomize.py. My guess is that you should put it in the same directory as the bulk of the Python standard library files. (Also in that directory is a subdirectory called site-packages, where you can put custom modules that will be available for import from any of your scripts.) -- http://mail.python.org/mailman/listinfo/python-list