[Answer]-Python3: disabling print statement depending on server

1👍

If you just want to stop all writing to stdout, you can do sys.stdout = None (or, if you want to be slightly more pedantic, sys.stdout = open(os.devnull)). As far as changing behavior depending on your environment, you may be able to distinguish between them based on the results of (say) socket.gethostname(). Alternatively, you can set an environment variable on either the server or your local box (but not both) and then test os.environ for the variable’s presence or value.

You may be better off using the built-in logging module instead of print() calls. That will allow much more fine-grained control over which things get logged and where the logs go.

Leave a comment