Just for those new to Python and to reduce what might confuse new coders; the original poster mentions/ed a "print() command". Python has statements which can not be overridden. Python also has functions; some built in. Python does not have any "commands". However the syntax "someword()" or in this case "print()" tells other Pythoneers that "someword" is a function, not a statement, because of the "()" ending parenthesis. Functions, even built-in ones, can be overridden. This comment is only intended to clarify.
–
DevPlayerApr 11 '12 at 13:26

If you are using 3.0, print is a function. If you are using 2.6, you can from __future__ import print_function and continue with a print function.

If <= 2.5, you can replace stdout like others have suggested, but be very careful if your wsgi server will call your app in multiple threads simultaneously. You WILL end up with simultaneous requests being sent down the same pipe.

It is worth noting that use of 'print' to sys.stdout in Apache/mod_wsgi was deliberately restricted. This is because a portable WSGI application should not use either sys.stdin or sys.stdout as some WSGI implementations use them to communicate to the server.

Apache/mod_wsgi is therefore trying to force you to write your WSGI application such that it will be portable to other WSGI implementations.

Unfortunately, too many people seem not to care about writing good code and so mod_wsgi 3.0 will allow you to write to sys.stdout and thus use 'print' without redirecting output to 'sys.stderr' as you should be doing.

Either way, the mod_wsgi documentation details how to remove the restriction in versions of mod_wsgi prior to 3.0. In particular, see documentation about the WSGIRestrictStdout directive. The documentation about debugging techniques also talks about the issue and about mapping sys.stdout to sys.stderr.

While you can redirect stdout to different sources like file for logging, as Paolo mentions, you probably wouldn't need it. I didn't need it. If you really needed to log stuff, you would be using logging itself in the first place, wouldn't you? Also, even when you don't print a thing, the third party libraries you use may do. Just redirect it and get going.

The simplest solution to this problem is to redirect all stdout into stderr. In the wsgi configuration file, just redirect as necessary.