473,386 Members | 1,842 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,386 software developers and data experts.

unexpected from/import statement behaviour

Hi all !

I'm trying to capture stderr of an external module I use in my python
program. I'm doing this
by setting up a class in my module overwriting the stderr file object
method write.
The external module outputs to stderr this way:

from sys import std err

.....

print >stderr, "Some text"

While in my module I use

import sys

..... sys.stderr ... sys.stdout

Well, as long as I do not change in the external module those from/
import statements to just

import sys

.....

print >sys.stderr, "Some text"

I'm not able to capture its stderr and of course I would like not to
do this kind of change.
I've always been convinced of the equivalence of the two ways of using
the import statement
but it's clear I'm wrong :-(

Please, someone can tell me what's going on ?

Thanks in advance !
Aug 27 '08 #1
10 1572
nisp schrieb:
Hi all !

I'm trying to capture stderr of an external module I use in my python
program. I'm doing this
by setting up a class in my module overwriting the stderr file object
method write.
The external module outputs to stderr this way:

from sys import std err

....

print >stderr, "Some text"

While in my module I use

import sys

..... sys.stderr ... sys.stdout

Well, as long as I do not change in the external module those from/
import statements to just

import sys

....

print >sys.stderr, "Some text"

I'm not able to capture its stderr and of course I would like not to
do this kind of change.
I've always been convinced of the equivalence of the two ways of using
the import statement
but it's clear I'm wrong :-(

Please, someone can tell me what's going on ?
http://effbot.org/zone/import-confusion.htm

Diez
Aug 27 '08 #2
nisp <emanuele.nesp...@gmail.comwrote:
I've always been convinced of the equivalence of the two ways of using
the import statement but it's clear I'm wrong :-(
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>from sys import stderr
import sys
sys.stderr is stderr
True

Behaviourly, I'm finding no difference between the two either.

Could you cut & paste a minimal example that isn't producing the
correct behaviour, and perhaps mention what type of OS you're using?
Aug 27 '08 #3
alex23 schrieb:
nisp <emanuele.nesp...@gmail.comwrote:
>I've always been convinced of the equivalence of the two ways of using
the import statement but it's clear I'm wrong :-(

Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>from sys import stderr
import sys
sys.stderr is stderr
True

Behaviourly, I'm finding no difference between the two either.

Could you cut & paste a minimal example that isn't producing the
correct behaviour, and perhaps mention what type of OS you're using?
You did not read this part of the post:

"""
I'm not able to capture its stderr and of course I would like not to
do this kind of change.
"""

He tries to set sys.stderr to a new stream and capture the print
statements, but fails to do so because he created a local alias.
Diez
Aug 27 '08 #4
nisp wrote:
Hi all !

I'm trying to capture stderr of an external module I use in my python
program. I'm doing this
by setting up a class in my module overwriting the stderr file object
method write.
The external module outputs to stderr this way:

from sys import std err

....

print >stderr, "Some text"

While in my module I use

import sys

..... sys.stderr ... sys.stdout

Well, as long as I do not change in the external module those from/
import statements to just

import sys

....

print >sys.stderr, "Some text"

I'm not able to capture its stderr and of course I would like not to
do this kind of change.
I've always been convinced of the equivalence of the two ways of using
the import statement
but it's clear I'm wrong :-(

Please, someone can tell me what's going on ?

Thanks in advance !
A practical approach to complement Diez' link to the explanation:

Instead of modifying the external module you can either redirect stderr
before you import the external module

import sys
sys.stderr = whatever
import external

or monkey-patch:

import sys
import external

sys.stderr = external.sterr = whatever

Peter

Aug 27 '08 #5
On Aug 27, 9:56 am, Peter Otten <__pete...@web.dewrote:
nisp wrote:
Hi all !
I'm trying to capture stderr of an external module I use in my python
program. I'm doing this
by setting up a class in my module overwriting the stderr file object
method write.
The external module outputs to stderr this way:
from sys import std err
....
print >stderr, "Some text"
While in my module I use
import sys
..... sys.stderr ... sys.stdout
Well, as long as I do not change in the external module those from/
import statements to just
import sys
....
print >sys.stderr, "Some text"
I'm not able to capture its stderr and of course I would like not to
do this kind of change.
I've always been convinced of the equivalence of the two ways of using
the import statement
but it's clear I'm wrong :-(
Please, someone can tell me what's going on ?
Thanks in advance !

A practical approach to complement Diez' link to the explanation:

Instead of modifying the external module you can either redirect stderr
before you import the external module

import sys
sys.stderr = whatever
import external

or monkey-patch:

import sys
import external

sys.stderr = external.sterr = whatever

Peter
Hi all !

Thanks first of all ! I read the interesting Diez's link but something
still remains to me unclear, on the other hand it's clear the my
problem is concentrated there and on symbols.
Here is what I'm trying to do

HelloWorld.py: this is a real simplification of my external module
though still reflecting its structure (commented out is the version
that, let's say, works)

from sys import stderr
#import sys

class Cheers:
def __init__(self):
self.cheersMsg = 'Hello World !!'
print "Cheers stderr %s" % stderr
#print "Cheers stderr %s" % sys.stderr
def _putCheers(self):
print>>stderr, 'Here is my msg:', self.cheersMsg
print>>stderr, 'This is a nice day today !!'
#print>>sys.stderr, 'Here is my msg:', self.cheersMsg
#print>>sys.stderr, 'This is a nice day today !!'
def doSomeStuff(self):
self._putCheers()

And below there is the module that uses the above one (mymodule.py):

#!/usr/bin/python

import sys
from HelloWorld import *

class StderrCatcher:
def __init__(self):
self.data = ''
def write(self,stuff):
self.data = self.data + "\t" + stuff
def main():

print "mymodule stderr: %s" % sys.stderr

sys.stderr = stderr = StderrCatcher()
m = Cheers()
m.doSomeStuff()
print "stderr: \n%s" % sys.stderr.data

if __name__ == '__main__':
main()
Below there is the output when it doesn't work:

mymodule stderr: <open file '<stderr>', mode 'w' at 0xb7d160b0>
Cheers stderr <open file '<stderr>', mode 'w' at 0xb7d160b0>
Here is my msg: Hello World !!
This is a nice day today !!
stderr:

And here when it works:

mymodule stderr: <open file '<stderr>', mode 'w' at 0xb7dc40b0>
Cheers stderr <__main__.StderrCatcher instance at 0xb7d8bd4c>
stderr:
Here is my msg: Hello World !!
This is a nice day today !!
Thanks again!

PS Sorry for having probably replied to somone of you directly :-(
Aug 27 '08 #6
nisp wrote:
On Aug 27, 9:56 am, Peter Otten <__pete...@web.dewrote:
>nisp wrote:
Hi all !
I'm trying to capture stderr of an external module I use in my python
program. I'm doing this
by setting up a class in my module overwriting the stderr file object
method write.
The external module outputs to stderr this way:
from sys import std err
....
print >stderr, "Some text"
While in my module I use
import sys
..... sys.stderr ... sys.stdout
Well, as long as I do not change in the external module those from/
import statements to just
import sys
....
print >sys.stderr, "Some text"
I'm not able to capture its stderr and of course I would like not to
do this kind of change.
I've always been convinced of the equivalence of the two ways of using
the import statement
but it's clear I'm wrong :-(
Please, someone can tell me what's going on ?
Thanks in advance !

A practical approach to complement Diez' link to the explanation:

Instead of modifying the external module you can either redirect stderr
before you import the external module

import sys
sys.stderr = whatever
import external

or monkey-patch:

import sys
import external

sys.stderr = external.sterr = whatever

Peter

Hi all !

Thanks first of all ! I read the interesting Diez's link but something
still remains to me unclear, on the other hand it's clear the my
problem is concentrated there and on symbols.
Here is what I'm trying to do

HelloWorld.py: this is a real simplification of my external module
though still reflecting its structure (commented out is the version
that, let's say, works)

from sys import stderr
This is your problem. You create a HelloWorld.stderr-alias to the object
bound to sys.stderr. Rebinding the latter won't affect the former. That is
precisely what the link I gave you explains.

The short answer to the whole issue is: dont' use the from-import syntax
until you really know what you are doing. Or not at all.

Diez
Aug 27 '08 #7
On Aug 27, 2:43 pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
nisp wrote:
On Aug 27, 9:56 am, Peter Otten <__pete...@web.dewrote:
nisp wrote:
Hi all !
I'm trying to capture stderr of an external module I use in my python
program. I'm doing this
by setting up a class in my module overwriting the stderr file object
method write.
The external module outputs to stderr this way:
from sys import std err
....
print >stderr, "Some text"
While in my module I use
import sys
..... sys.stderr ... sys.stdout
Well, as long as I do not change in the external module those from/
import statements to just
import sys
....
print >sys.stderr, "Some text"
I'm not able to capture its stderr and of course I would like not to
do this kind of change.
I've always been convinced of the equivalence of the two ways of using
the import statement
but it's clear I'm wrong :-(
Please, someone can tell me what's going on ?
Thanks in advance !
A practical approach to complement Diez' link to the explanation:
Instead of modifying the external module you can either redirect stderr
before you import the external module
import sys
sys.stderr = whatever
import external
or monkey-patch:
import sys
import external
sys.stderr = external.sterr = whatever
Peter
Hi all !
Thanks first of all ! I read the interesting Diez's link but something
still remains to me unclear, on the other hand it's clear the my
problem is concentrated there and on symbols.
Here is what I'm trying to do
HelloWorld.py: this is a real simplification of my external module
though still reflecting its structure (commented out is the version
that, let's say, works)
from sys import stderr

This is your problem. You create a HelloWorld.stderr-alias to the object
bound to sys.stderr. Rebinding the latter won't affect the former. That is
precisely what the link I gave you explains.

The short answer to the whole issue is: dont' use the from-import syntax
until you really know what you are doing. Or not at all.

Diez
Hi Diez! I well understand the problem now, the fact is that
unfortunately the external module is not mine and i doubt i can change
it :-( Anyway as far as i'm concerned i've learned the lesson ! Thanks
again and greetings,
Nisp
Aug 27 '08 #8
nisp wrote:
Thanks first of all ! I read the interesting Diez's link but something
still remains to me unclear, on the other hand it's clear the my
problem is concentrated there and on symbols.
Read it again. If you have two modules

module1.py
from sys import stderr

module2.py
from module1 import stderr

you get three names 'stderr' in three different namespaces, and each name
may bind a different object.

Here is what I'm trying to do

HelloWorld.py: this is a real simplification of my external module
though still reflecting its structure (commented out is the version
that, let's say, works)

from sys import stderr
#import sys

class Cheers:
Â*Â*Â*Â*Â*Â*Â*Â*def __init__(self):
Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*self.cheersMsg = 'Hello World !!'
Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*print "Cheers stderr %s" % stderr
Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*#print "Cheers stderr %s" % sys.stderr
Â*Â*Â*Â*Â*Â*Â*Â*def _putCheers(self):
Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*print>>stderr, 'Here is my msg:', self.cheersMsg
Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*print>>stderr, 'This is a nice day today !!'
Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*#print>>sys.stderr , 'Here is my msg:', self.cheersMsg
Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*#print>>sys.stderr , 'This is a nice day today !!'
Â*Â*Â*Â*Â*Â*Â*Â*def doSomeStuff(self):
Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*self._putCheers()

And below there is the module that uses the above one (mymodule.py):

#!/usr/bin/python

import sys
This imports HelloWorld.stderr:
from HelloWorld import *
You now have a global variable 'stderr' in module __main__, initialized to
the same value as HelloWorld.stderr.
class StderrCatcher:
Â*Â*Â*Â*Â*Â*Â*Â*def __init__(self):
Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*self.data = ''
Â*Â*Â*Â*Â*Â*Â*Â*def write(self,stuff):
Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*self.data = self.data + "\t" + stuff
def main():

Â*Â*Â*Â*Â*Â*Â*Â*print "mymodule stderr: %s" % sys.stderr
This rebinds sys.stderr and a local 'stderr' in main()
Â*Â*Â*Â*Â*Â*Â*Â*sys.stderr = stderr = StderrCatcher()
but both HelloWorld.stderr and __main__.stderr are unaffected. What you need
is
import HelloWorld
sys.stderr = HelloWorld.stderr = StderrCatcher()
Â*Â*Â*Â*Â*Â*Â*Â*m = Cheers()
Â*Â*Â*Â*Â*Â*Â*Â*m.doSomeStuff()
Â*Â*Â*Â*Â*Â*Â*Â*print "stderr: \n%s" % sys.stderr.data

if __name__ == '__main__':
Â* Â* main()
Below there is the output when it doesn't work:

mymodule stderr: <open file '<stderr>', mode 'w' at 0xb7d160b0>
Cheers stderr <open file '<stderr>', mode 'w' at 0xb7d160b0>
Here is my msg: Hello World !!
This is a nice day today !!
stderr:

And here when it works:

mymodule stderr: <open file '<stderr>', mode 'w' at 0xb7dc40b0>
Cheers stderr <__main__.StderrCatcher instance at 0xb7d8bd4c>
stderr:
Â* Â* Â* Â* Here is my msg: Â* Â* Â* Â* Hello World !!
Â* Â* Â* Â* This is a nice day today !!
Thanks again!

PS Sorry for having probably replied to somone of you directly :-(
Aug 27 '08 #9
On Aug 27, 5:42*pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
alex23 schrieb:
nisp <emanuele.nesp...@gmail.comwrote:
I've always been convinced of the equivalence of the two ways of using
the import statement but it's clear I'm wrong :-(
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>from sys import stderr
import sys
sys.stderr is stderr
True
Behaviourly, I'm finding no difference between the two either.
Could you cut & paste a minimal example that isn't producing the
correct behaviour, and perhaps mention what type of OS you're using?

You did not read this part of the post:

"""
I'm not able to capture its stderr and of course I would like not to
do this kind of change.
"""

He tries to set sys.stderr to a new stream and capture the print
statements, but fails to do so because he created a local alias.
No, I read it, I just totally didn't parse it that way :)

Sorry for the confusion, nisp.

Aug 27 '08 #10
On Aug 27, 3:35 pm, Peter Otten <__pete...@web.dewrote:
nisp wrote:
Thanks first of all ! I read the interesting Diez's link but something
still remains to me unclear, on the other hand it's clear the my
problem is concentrated there and on symbols.

Read it again. If you have two modules

module1.py
from sys import stderr

module2.py
from module1 import stderr

you get three names 'stderr' in three different namespaces, and each name
may bind a different object.
Here is what I'm trying to do
HelloWorld.py: this is a real simplification of my external module
though still reflecting its structure (commented out is the version
that, let's say, works)
from sys import stderr
#import sys
class Cheers:
def __init__(self):
self.cheersMsg = 'Hello World !!'
print "Cheers stderr %s" % stderr
#print "Cheers stderr %s" % sys.stderr
def _putCheers(self):
print>>stderr, 'Here is my msg:', self.cheersMsg
print>>stderr, 'This is a nice day today !!'
#print>>sys.stderr, 'Here is my msg:', self.cheersMsg
#print>>sys.stderr, 'This is a nice day today !!'
def doSomeStuff(self):
self._putCheers()
And below there is the module that uses the above one (mymodule.py):
#!/usr/bin/python
import sys

This imports HelloWorld.stderr:
from HelloWorld import *

You now have a global variable 'stderr' in module __main__, initialized to
the same value as HelloWorld.stderr.
class StderrCatcher:
def __init__(self):
self.data = ''
def write(self,stuff):
self.data = self.data + "\t" + stuff
def main():
print "mymodule stderr: %s" % sys.stderr

This rebinds sys.stderr and a local 'stderr' in main()
sys.stderr = stderr = StderrCatcher()

but both HelloWorld.stderr and __main__.stderr are unaffected. What you need
is
import HelloWorld
sys.stderr = HelloWorld.stderr = StderrCatcher()
m = Cheers()
m.doSomeStuff()
print "stderr: \n%s" % sys.stderr.data
if __name__ == '__main__':
main()
Below there is the output when it doesn't work:
mymodule stderr: <open file '<stderr>', mode 'w' at 0xb7d160b0>
Cheers stderr <open file '<stderr>', mode 'w' at 0xb7d160b0>
Here is my msg: Hello World !!
This is a nice day today !!
stderr:
And here when it works:
mymodule stderr: <open file '<stderr>', mode 'w' at 0xb7dc40b0>
Cheers stderr <__main__.StderrCatcher instance at 0xb7d8bd4c>
stderr:
Here is my msg: Hello World !!
This is a nice day today !!
Thanks again!
PS Sorry for having probably replied to somone of you directly :-(
Hi Peter!

Definitely you are right ! May be this time I understood completely
the topic (or may be not ?... gosh !) ... I didn't considered well the
"from HelloWolrd import *" in mymodule.py.
Now my module is:

import sys
import HelloWorld

class StderrCatcher:
def __init__(self):
self.data = ''
def write(self,stuff):
self.data = self.data + "\t" + stuff
def main():

print "mymodule stderr: %s" % sys.stderr

mystderr = sys.stderr = HelloWorld.stderr = StderrCatcher()
m = HelloWorld.Cheers()
m.doSomeStuff()
print >>sys.stderr, "mymodule prints something"
print "stderr: \n%s" % mystderr.data

if __name__ == '__main__':
main()

and its output is:

mymodule stderr: <open file '<stderr>', mode 'w' at 0xb7d2d0b0>
Cheers stderr <__main__.StderrCatcher instance at 0xb7cf4e4c>
stderr:
Here is my msg: Hello World !!
This is a nice day today !!
mymodule prints something

now it works perfectly !

Great ! Thanks to all of you !

Nisp
Aug 28 '08 #11

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

Similar topics

2
by: CK | last post by:
I am a "newbie" to python and today I had the need to write a program which generated a lot of tcp connections to a range of addresses (10.34.32.0/22) in order to troubleshoot a problem with a...
2
by: Gerhard Esterhuizen | last post by:
Hi, I am observing unexpected behaviour, in the form of a corrupted class member access, from a simple C++ program that accesses an attribute declared in a virtual base class via a chain of...
16
by: didier.doussaud | last post by:
I have a stange side effect in my project : in my project I need to write "gobal" to use global symbol : .... import math .... def f() : global math # necessary ?????? else next line...
6
by: Sakcee | last post by:
html = '<html><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" <head></head> <body bgcolor=#ffffff>\r\n Foo foo , blah blah </body></html>' >>> import htmllib >>> import...
3
by: rimmer | last post by:
I'm writing an extension module in C in which I'm passing an array of floats from C to python. The code below illustrates a simple C function designed to output an array of floats. ---------...
4
by: conan | last post by:
This regexp '<widget class=".*" id=".*">' works well with 'grep' for matching lines of the kind <widget class="GtkWindow" id="window1"> on a XML .glade file However that's not true for the...
20
by: TC | last post by:
I need an automated procedure to copy data from an Access table to a SQL Server table. Speed is important. What is the recommended technique? I can export the data from Access, copy it via FTP,...
7
by: Andrew McLean | last post by:
I have a bunch of csv files that have the following characteristics: - field delimiter is a comma - all fields quoted with double quotes - lines terminated by a *space* followed by a newline ...
3
by: rs387 | last post by:
Hi, I've found the following behaviour on importing a variable from a module somewhat odd. The behaviour is identical in Python 2.5 and 3.0b2. In summary, here's what happens. I have a...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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,...
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...

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.