Redirecting standard out in a single namespace

Farshid Lashkari lashkariNO at
Fri Jan 20 14:42:44 EST 2006

Fuzzyman wrote:
> Is there another way to shadow the sys module from a single namespace ?

I've never actually tried this, but try making a copy of the sys module 
then replacing the stdout object with your own object. Then you can 
replace "sys" of the namespace with your custom "sys" module. I guess it 
would look something like this:

import mysys     #My dummy sys module
import sys       #Actual sys module

#Copy sys module into dummy sys module

#Replace stdout with custom stdout
mysys.stdout = MyStdout()

#Replace sys of namespace
namespace.sys = mysys

This seems like it should work, however I don't know if copying the 
entire dictionary of one module to another is a safe thing to do.


More information about the Python-list mailing list