473,396 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

reload(sys)

Hello,

I've had an encoding issue and solved it by
"sys.setdefaultencoding('utf-8')"...

My first try wasn't successful since setdefaultencoding is not named
when I imported sys module. After, I import sys module, I needed to
write "reload(sys)" also.

I wonder why we need to call "reload(sys)" to get setdefaultencoding
named?

Happy coding

Aug 31 '07 #1
11 19484
Sönmez Kartal wrote:
I've had an encoding issue and solved it by
"sys.setdefaultencoding('utf-8')"...

My first try wasn't successful since setdefaultencoding is not named
when I imported sys module. After, I import sys module, I needed to
write "reload(sys)" also.

I wonder why we need to call "reload(sys)" to get setdefaultencoding
named?
sys.setdefaultencoding is purposely deleted from the sys module after
it's loaded because you really shouldn't be using it. The reload() call
restores the deleted attribute.

If you'd like a less brittle solution to your encoding issue, explain
what the issue was, and people here can probably help you find a better
solution.

STeVe
Aug 31 '07 #2
On 31 A ustos, 04:24, Steven Bethard <steven.beth...@gmail.comwrote:
Sönmez Kartal wrote:
I've had an encoding issue and solved it by
"sys.setdefaultencoding('utf-8')"...
My first try wasn't successful since setdefaultencoding is not named
when I imported sys module. After, I import sys module, I needed to
write "reload(sys)" also.
I wonder why we need to call "reload(sys)" to get setdefaultencoding
named?

sys.setdefaultencoding is purposely deleted from the sys module after
it's loaded because you really shouldn't be using it. The reload() call
restores the deleted attribute.

If you'd like a less brittle solution to your encoding issue, explain
what the issue was, and people here can probably help you find a better
solution.

STeVe
I was using the XMLBuilder(xmlbuilder.py). I'm writing XML files as
"f.write(str(xml))". At execution of that line, it gives error with
description, configure your default encoding... My operating system's
default is utf-8, and Emacs' is utf-8 too. Default of XMLBuilder is
utf-8 too. There were some characters interpreter may couldn't print
in ascii. I have tried to replace those characters like (TM) (R)... I
cannot remember them right now, but if necessary I can find them
easily...

This is the part of xmlbuilder.py which raises the error.

try:
if self.pretty:
# tabs are evil, so we will use two spaces
outstr = self._dom.toprettyxml("
",encoding=self.encoding)
else:
outstr = self._dom.toxml(encoding=self.encoding)
except UnicodeDecodeError:
sys.stderr.write('Decoding Error: You must configure
default encoding\n')
sys.exit()

What I can do instead of "import sys; reload(sys);
sys.setdefaultencoding('utf-8')"?

Happy coding

Aug 31 '07 #3
On Fri, 31 Aug 2007 12:53:36 +0000, Sönmez Kartal wrote:
On 31 A ustos, 04:24, Steven Bethard <steven.beth...@gmail.comwrote:
>Snmez Kartal wrote:
I've had an encoding issue and solved it by
"sys.setdefaultencoding('utf-8')"...
My first try wasn't successful since setdefaultencoding is not named
when I imported sys module. After, I import sys module, I needed to
write "reload(sys)" also.
I wonder why we need to call "reload(sys)" to get setdefaultencoding
named?

sys.setdefaultencoding is purposely deleted from the sys module after
it's loaded because you really shouldn't be using it. The reload() call
restores the deleted attribute.

If you'd like a less brittle solution to your encoding issue, explain
what the issue was, and people here can probably help you find a better
solution.

I was using the XMLBuilder(xmlbuilder.py). I'm writing XML files as
"f.write(str(xml))". At execution of that line, it gives error with
description, configure your default encoding...
This doesn't help us that much. What is `f` here and what is `xml`?
This is the part of xmlbuilder.py which raises the error.

try:
if self.pretty:
# tabs are evil, so we will use two spaces
outstr = self._dom.toprettyxml("
",encoding=self.encoding)
else:
outstr = self._dom.toxml(encoding=self.encoding)
except UnicodeDecodeError:
sys.stderr.write('Decoding Error: You must configure
default encoding\n')
sys.exit()
So there is an attribute `self.encoding` on that object. Is it set? What
encoding is it? And do you put byte strings with values outside ASCII
into your XML or unicode strings?

Ciao,
Marc 'BlackJack' Rintsch
Aug 31 '07 #4
Sönmez Kartal wrote:
On 31 A ustos, 04:24, Steven Bethard <steven.beth...@gmail.comwrote:
>Sönmez Kartal wrote:
>>I've had an encoding issue and solved it by
"sys.setdefaultencoding('utf-8')"...
My first try wasn't successful since setdefaultencoding is not named
when I imported sys module. After, I import sys module, I needed to
write "reload(sys)" also.
I wonder why we need to call "reload(sys)" to get setdefaultencoding
named?
sys.setdefaultencoding is purposely deleted from the sys module after
it's loaded because you really shouldn't be using it. The reload() call
restores the deleted attribute.

If you'd like a less brittle solution to your encoding issue, explain
what the issue was, and people here can probably help you find a better
solution.

STeVe

I was using the XMLBuilder(xmlbuilder.py). I'm writing XML files as
"f.write(str(xml))". At execution of that line, it gives error with
description, configure your default encoding...
Could you post the actual traceback you're getting?

STeVe
Aug 31 '07 #5
On 31 A ustos, 16:58, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
On Fri, 31 Aug 2007 12:53:36 +0000, Sönmez Kartal wrote:
On 31 A ustos, 04:24, Steven Bethard <steven.beth...@gmail.comwrote:
Snmez Kartal wrote:
I've had an encoding issue and solved it by
"sys.setdefaultencoding('utf-8')"...
My first try wasn't successful since setdefaultencoding is not named
when I imported sys module. After, I import sys module, I needed to
write "reload(sys)" also.
I wonder why we need to call "reload(sys)" to get setdefaultencoding
named?
sys.setdefaultencoding is purposely deleted from the sys module after
it's loaded because you really shouldn't be using it. The reload() call
restores the deleted attribute.
If you'd like a less brittle solution to your encoding issue, explain
what the issue was, and people here can probably help you find a better
solution.
I was using the XMLBuilder(xmlbuilder.py). I'm writing XML files as
"f.write(str(xml))". At execution of that line, it gives error with
description, configure your default encoding...

This doesn't help us that much. What is `f` here and what is `xml`?
This is the part of xmlbuilder.py which raises the error.
try:
if self.pretty:
# tabs are evil, so we will use two spaces
outstr = self._dom.toprettyxml("
",encoding=self.encoding)
else:
outstr = self._dom.toxml(encoding=self.encoding)
except UnicodeDecodeError:
sys.stderr.write('Decoding Error: You must configure
default encoding\n')
sys.exit()

So there is an attribute `self.encoding` on that object. Is it set? What
encoding is it? And do you put byte strings with values outside ASCII
into your XML or unicode strings?

Ciao,
Marc 'BlackJack' Rintsch
I should have said that 'f' is a file object and xml is a XMLBuilder
object. Sorry. :-)

self.encoding is 'utf-8' by default.

I have only ® and ™ characters in the XML file and a space character
which Emacs shows as colored '_'. I have replaced those but didn't
work!

Here is the full code of xmlbuilder.py: http://rafb.net/p/9rURi822.html

I don't wanna bother you but if you see there is something not
practical then I'll keep writing about this. :-)

Sep 1 '07 #6
On 31 A ustos, 20:09, Steven Bethard <steven.beth...@gmail.comwrote:
Sönmez Kartal wrote:
On 31 A ustos, 04:24, Steven Bethard <steven.beth...@gmail.comwrote:
Sönmez Kartal wrote:
I've had an encoding issue and solved it by
"sys.setdefaultencoding('utf-8')"...
My first try wasn't successful since setdefaultencoding is not named
when I imported sys module. After, I import sys module, I needed to
write "reload(sys)" also.
I wonder why we need to call "reload(sys)" to get setdefaultencoding
named?
sys.setdefaultencoding is purposely deleted from the sys module after
it's loaded because you really shouldn't be using it. The reload() call
restores the deleted attribute.
If you'd like a less brittle solution to your encoding issue, explain
what the issue was, and people here can probably help you find a better
solution.
STeVe
I was using the XMLBuilder(xmlbuilder.py). I'm writing XML files as
"f.write(str(xml))". At execution of that line, it gives error with
description, configure your default encoding...

Could you post the actual traceback you're getting?

STeVe
Steve,
I get this when it happens: "Decoding Error: You must configure
default encoding" which comes from in the code excerpt in
xmlbuilder.py (http://rafb.net/p/9rURi822.html)
Sep 1 '07 #7
Sönmez Kartal wrote:
I was using the XMLBuilder(xmlbuilder.py). I'm writing XML files as
"f.write(str(xml))". At execution of that line, it gives error with
description, configure your default encoding...
[and later]
>
I get this when it happens: "Decoding Error: You must configure
default encoding" which comes from in the code excerpt in
xmlbuilder.py (http://rafb.net/p/9rURi822.html)
Can you show the code where you populate the XMLBuilder? I'm guessing
you're doing something like::

import xmlbuilder
builder = xmlbuilder.XMLBuilder()
builder.foo = dict(bar='® and ™')
str(builder)

That breaks because the string '® and ™' is not properly encoded. Have
you declared an encoding in your source file? PEP 263 shows you how:

http://www.python.org/dev/peps/pep-0263/

Note that with Python 2.5 the code above gives a SyntaxError without a
proper encoding. You should also probably be prefixing your string
literals containing weird characters with "u" to make them unicode.
Doing both of these in the code above made it work for me.

STeVe
Sep 3 '07 #8
On 3 Eylül, 05:40, Steven Bethard <steven.beth...@gmail.comwrote:
Sönmez Kartal wrote:
I was using the XMLBuilder(xmlbuilder.py). I'm writing XML files as
"f.write(str(xml))". At execution of that line, it gives error with
description, configure your default encoding...

[and later]
I get this when it happens: "Decoding Error: You must configure
default encoding" which comes from in the code excerpt in
xmlbuilder.py (http://rafb.net/p/9rURi822.html)

Can you show the code where you populate the XMLBuilder? I'm guessing
you're doing something like::

import xmlbuilder
builder = xmlbuilder.XMLBuilder()
builder.foo = dict(bar='® and ™')
str(builder)

That breaks because the string '® and ™' is not properly encoded. Have
you declared an encoding in your source file? PEP 263 shows you how:

http://www.python.org/dev/peps/pep-0263/

Note that with Python 2.5 the code above gives a SyntaxError without a
proper encoding. You should also probably be prefixing your string
literals containing weird characters with "u" to make them unicode.
Doing both of these in the code above made it work for me.

STeVe
http://rafb.net/p/RfaF8215.html

products in the code is a list of dictionaries which are returned by
makeProduct function.

I'm not typing or pasting those characters into my script. So,
declaring an encoding didn't make it. :-( But, your code excerpt
runned well.

I have tried "f.write(unicode(xml))" for the last line of the script.
No success.

I think we should think about how we can achieve the effect of
"sys.setdefaultencoding('utf-8')"

Sep 6 '07 #9
On 6 sep, 08:13, Sönmez Kartal <rainwatch...@gmail.comwrote:
On 3 Eylül, 05:40, Steven Bethard <steven.beth...@gmail.comwrote:
Sönmez Kartal wrote:
I was using the XMLBuilder(xmlbuilder.py). I'm writing XML files as
"f.write(str(xml))". At execution of that line, it gives error with
description, configure your default encoding...
Can you show the code where you populate the XMLBuilder? I'm guessing
you're doing something like::
import xmlbuilder
builder = xmlbuilder.XMLBuilder()
builder.foo = dict(bar='® and ™')
str(builder)
That breaks because the string '® and ™' is not properly encoded. Have
you declared an encoding in your source file? PEP 263 shows you how:
http://rafb.net/p/RfaF8215.html

products in the code is a list of dictionaries which are returned by
makeProduct function.

I'm not typing or pasting those characters into my script. So,
declaring an encoding didn't make it. :-( But, your code excerpt
runned well.
You should ensure that arguments to makeProduct are either:
- unicode objects
- ASCII strings

If you got them from some other place, decode the strings as soon as
possible into unicode. Read <http://www.amk.ca/python/howto/unicode>
to understand what's happening (and why the XMLBuilder error message
is *not* a good advice)

--
Gabriel Genellina

Sep 6 '07 #10
Sönmez Kartal wrote:
I was using the XMLBuilder(xmlbuilder.py). I'm writing XML files as
"f.write(str(xml))". At execution of that line, it gives error with
description, configure your default encoding...
[and later]
>
http://rafb.net/p/RfaF8215.html

products in the code is a list of dictionaries which are returned by
makeProduct function.

I'm not typing or pasting those characters into my script. So,
declaring an encoding didn't make it. :-( But, your code excerpt
runned well.
Gabriel Genellina wrote:
You should ensure that arguments to makeProduct are either:
- unicode objects
- ASCII strings

If you got them from some other place, decode the strings as soon as
possible into unicode. Read <http://www.amk.ca/python/howto/unicode>
to understand what's happening
To further illustrate Gabriel's point, here is some code where I read in
some UTF8 text from a file. If you properly decode that text from UTF8,
you don't get any errors. If you forget to decode that text, you'll get
exactly the "default encoding" error you were getting before:
>>f = open('temp.txt', 'w')
f.write(u'® and ™'.encode('utf8'))
f.close()
non_decoded_text = open('temp.txt').read()
decoded_text = non_decoded_text.decode('utf8')
import xmlbuilder
builder = xmlbuilder.XMLBuilder()
builder.foo = dict(bar=non_decoded_text)
str(builder)
Decoding Error: You must configure default encoding
>>builder = xmlbuilder.XMLBuilder()
builder.foo = dict(bar=decoded_text)
str(builder)
'<?xml version="1.0" encoding="utf-8"?><foo><bar>\xc2\xae and
\xc2\x99</bar></foo>'

Note that I didn't have to do anything with the default encoding. I
simply had to decode the text file with the appropriate codec. So,
looking at your code, I'm guessing that you need to figure out where
you're reading in the "name", "url" and "image" values, and make sure
you're properly decoding that text.

STeVe

P.S. If you can find somewhere to file a bug report for XMLBuilder, you
really should. The error instructing you to configure the default
encoding is really just wrong.
Sep 6 '07 #11
On 6 Eylül, 19:19, Steven Bethard <steven.beth...@gmail.comwrote:
Sönmez Kartal wrote:
I was using the XMLBuilder(xmlbuilder.py). I'm writing XML files as
"f.write(str(xml))". At execution of that line, it gives error with
description, configure your default encoding...

[and later]
http://rafb.net/p/RfaF8215.html
products in the code is a list of dictionaries which are returned by
makeProduct function.
I'm not typing or pasting those characters into my script. So,
declaring an encoding didn't make it. :-( But, your code excerpt
runned well.
Gabriel Genellina wrote:
You should ensure that arguments to makeProduct are either:
- unicode objects
- ASCII strings
If you got them from some other place, decode the strings as soon as
possible into unicode. Read <http://www.amk.ca/python/howto/unicode>
to understand what's happening

To further illustrate Gabriel's point, here is some code where I read in
some UTF8 text from a file. If you properly decode that text from UTF8,
you don't get any errors. If you forget to decode that text, you'll get
exactly the "default encoding" error you were getting before:
>>f = open('temp.txt', 'w')
>>f.write(u'® and ™'.encode('utf8'))
>>f.close()
>>non_decoded_text = open('temp.txt').read()
>>decoded_text = non_decoded_text.decode('utf8')
>>import xmlbuilder
>>builder = xmlbuilder.XMLBuilder()
>>builder.foo = dict(bar=non_decoded_text)
>>str(builder)
Decoding Error: You must configure default encoding
>>builder = xmlbuilder.XMLBuilder()
>>builder.foo = dict(bar=decoded_text)
>>str(builder)
'<?xml version="1.0" encoding="utf-8"?><foo><bar>\xc2\xae and
\xc2\x99</bar></foo>'

Note that I didn't have to do anything with the default encoding. I
simply had to decode the text file with the appropriate codec. So,
looking at your code, I'm guessing that you need to figure out where
you're reading in the "name", "url" and "image" values, and make sure
you're properly decoding that text.

STeVe

P.S. If you can find somewhere to file a bug report for XMLBuilder, you
really should. The error instructing you to configure the default
encoding is really just wrong.
Thank you Steve for your answers and Gabriel for reminding Unicode
HOWTO to me which I'm going to read tomorrow!

Happy coding

Sep 6 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

66
by: Ellinghaus, Lance | last post by:
> > Other surprises: Deprecating reload() >Reload doesn't work the way most people think >it does: if you've got any references to the old module, >they stay around. They aren't replaced. ...
1
by: Emmanuel | last post by:
Hi, I use a 'reload all' feature in my app, that allow to reload every module. I first try this version : import sys def Reload():
2
by: Robin Becker | last post by:
We had some legacy applications that used import to get parts of documents in. When run separately these worked fine, but failed when run as a single process because they both imported ch1 (after...
0
by: Mustafa Thamer | last post by:
Hi, I'm using import hooks according to PEP 302, in order to load python files from a game PAK file. The game is C++ using embedded and extended Python (v2.33) and Boost. The importing works...
2
by: Uwe Mayer | last post by:
Hi, well, I wrote a nice python program which won't work if the default encoding has not been set from ascii to latin-1 or latin-15. However, the command sys.setappdefaultencoding is missing...
4
by: Lonnie Princehouse | last post by:
So, it turns out that reload() fails if the module being reloaded isn't in sys.path. Maybe it could fall back to module.__file__ if the module isn't found in sys.path?? .... or reload could...
2
by: gen_tricomi | last post by:
Python 2.4.2 (#67, Sep 28 2005, 12:41:11) on win32 Type "copyright", "credits" or "license()" for more information. **************************************************************** Personal...
2
by: dmitrey | last post by:
my Python module was changed in HDD (hardware disk drive), moreover, changed its location (but still present in sys.path). how can I reload a func "myfunc" from the module? (or howto reload whole...
0
by: Rafe | last post by:
Hi, This seems to be an old question, and I've read back a bit, but rather than assume the answer is "you can't do that", I'd thought I'd post my version of the question along with a...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.