473,554 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reload(sys)

Hello,

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

My first try wasn't successful since setdefaultencod ing 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 setdefaultencod ing
named?

Happy coding

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

My first try wasn't successful since setdefaultencod ing 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 setdefaultencod ing
named?
sys.setdefaulte ncoding 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.setdefault encoding('utf-8')"...
My first try wasn't successful since setdefaultencod ing 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 setdefaultencod ing
named?

sys.setdefaulte ncoding 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(xmlb uilder.py). I'm writing XML files as
"f.write(str(xm l))". 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.topre ttyxml("
",encoding=self .encoding)
else:
outstr = self._dom.toxml (encoding=self. encoding)
except UnicodeDecodeEr ror:
sys.stderr.writ e('Decoding Error: You must configure
default encoding\n')
sys.exit()

What I can do instead of "import sys; reload(sys);
sys.setdefaulte ncoding('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.setdefault encoding('utf-8')"...
My first try wasn't successful since setdefaultencod ing 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 setdefaultencod ing
named?

sys.setdefault encoding 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(xmlb uilder.py). I'm writing XML files as
"f.write(str(xm l))". 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.topre ttyxml("
",encoding=self .encoding)
else:
outstr = self._dom.toxml (encoding=self. encoding)
except UnicodeDecodeEr ror:
sys.stderr.writ e('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.setdefau ltencoding('utf-8')"...
My first try wasn't successful since setdefaultencod ing 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 setdefaultencod ing
named?
sys.setdefault encoding 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(xmlb uilder.py). I'm writing XML files as
"f.write(str(xm l))". 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.net wrote:
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.setdefault encoding('utf-8')"...
My first try wasn't successful since setdefaultencod ing 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 setdefaultencod ing
named?
sys.setdefaulte ncoding 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(xmlb uilder.py). I'm writing XML files as
"f.write(str(xm l))". 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.topre ttyxml("
",encoding=self .encoding)
else:
outstr = self._dom.toxml (encoding=self. encoding)
except UnicodeDecodeEr ror:
sys.stderr.writ e('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.setdefaul tencoding('utf-8')"...
My first try wasn't successful since setdefaultencod ing 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 setdefaultencod ing
named?
sys.setdefaulte ncoding 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(xmlb uilder.py). I'm writing XML files as
"f.write(str(xm l))". 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(xmlb uilder.py). I'm writing XML files as
"f.write(str(xm l))". 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.XMLB uilder()
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(xmlb uilder.py). I'm writing XML files as
"f.write(str(xm l))". 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.XMLB uilder()
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(unicod e(xml))" for the last line of the script.
No success.

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

Sep 6 '07 #9
On 6 sep, 08:13, Sönmez Kartal <rainwatch...@g mail.comwrote:
On 3 Eylül, 05:40, Steven Bethard <steven.beth... @gmail.comwrote :
Sönmez Kartal wrote:
I was using the XMLBuilder(xmlb uilder.py). I'm writing XML files as
"f.write(str(xm l))". 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.XMLB uilder()
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

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

Similar topics

66
3844
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. >It was a good idea, but the implementation simply >doesn't do what the idea promises. I agree that it does not really work as most people think it...
1
2135
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
1951
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 jumping to their home dirs and placing these on the path). Clearly the first to run used up ch1. I have a simple test script below. It seems I...
0
1308
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 fine, but after modules are imported I can't reload them. I've tried 'reload(foo)' and 'PyImport_ReloadModule(pModPtr)', but both return 'ImportError:...
2
1930
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 on a Python installation with Python 2.3.4 on Gentoo where it is present on Debian. Any ideas how to deal with this?
4
4637
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 just take an optional path parameter... Or perhaps I'm the only one who thinks this is silly: >>> my_module = imp.load_module(module_name,...
2
2813
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 firewall software may warn about the connection IDLE makes to its subprocess using this computer's internal loopback interface. This connection is...
2
2748
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 module)? Thank you in advance, D.
0
1891
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 reproducible error to illustrate my confusion. My problem is that I'm using Python inside XSI (a 3D graphics application). If I want to restart Python, I...
0
7605
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7530
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7808
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8047
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7570
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7893
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5162
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2022
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1141
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.