How to block calls to print?


Is there a way to stop a function from calling print?

I am using the pygame.joystick module for a game I am working on.

I created a pygame.joystick.Joystick object and in the actual loop of the game call its member function get_button to check for user input. The function does everything I need it to do, but the problem is that it also calls print, which slows down the game considerably.

Can I block this call to print?

Asked By: dmlicht



No, there is not, especially that majority of PyGame is written in C.

But if this function calls print, then it’s PyGame bug, and you should just report it.

Answered By: Cat Plus Plus

I have had the same problem, and I did not come to another solution but to redirect the output of the program (I don’t know exactly whether the spamming happens on stdout or stderr) to /dev/null nirvana.

Indeed, it’s open source, but I wasn’t passionate enough to dive into the pygame sources – and the build process – to somehow stop the debug spam.


The pygame.joystick module has calls to printf in all functions that return the actual values to Python:

printf("SDL_JoystickGetButton value:%d:n", value);

Unfortunately you would need to comment these out and recompile the whole thing. Maybe the provided would make this easier than I thought. You could try this…

Answered By: moooeeeep

Python lets you overwrite standard output (stdout) with any file object. This should work cross platform and write to the null device.

import sys, os

# Disable
def blockPrint():
    sys.stdout = open(os.devnull, 'w')

# Restore
def enablePrint():
    sys.stdout = sys.__stdout__

print 'This will print'

print "This won't"

print "This will too"

If you don’t want that one function to print, call blockPrint() before it, and enablePrint() when you want it to continue. If you want to disable all printing, start blocking at the top of the file.

Answered By: Brigand

A completely different approach would be redirecting at the command line. If you’re on Windows, this means a batch script. On Linux, bash.

/full/path/to/my/game/ > /dev/null
C:FullPathToMyGame.exe > nul

Unless you’re dealing with multiple processes, this should work. For Windows users this could be the shortcuts you’re creating (start menu / desktop).

Answered By: Brigand

The module I used printed to stderr. So the solution in that case would be:

sys.stdout = open(os.devnull, 'w')
Answered By: David Schumann

Use with

Based on @FakeRainBrigand solution I’m suggesting a safer solution:

import os, sys

class HiddenPrints:
    def __enter__(self):
        self._original_stdout = sys.stdout
        sys.stdout = open(os.devnull, 'w')

    def __exit__(self, exc_type, exc_val, exc_tb):
        sys.stdout = self._original_stdout

Then you can use it like this:

with HiddenPrints():
    print("This will not be printed")

print("This will be printed as before")

This is much safer because you can not forget to re-enable stdout, which is especially critical when handling exceptions.

Without with — Bad practice

The following example uses enable/disable prints functions that were suggested in previous answer.

Imagine that there is a code that may raise an exception. We had to use finally statement in order to enable prints in any case.

    enable_prints() # This will not help in case of exception
except ValueError as err:
    enable_prints() # That's where it needs to go.

If you forgot the finally clause, none of your print calls would print anything anymore.

It is safer to use the with statement, which makes sure that prints will be reenabled.

Note: It is not safe to use sys.stdout = None, because someone could call methods like sys.stdout.write()

Answered By: Alexander C

As @Alexander Chzhen suggested, using a context manager would be safer than calling a pair of state-changing functions.

However, you don’t need to reimplement the context manager – it’s already in the standard library. You can redirect stdout (the file object that print uses) with contextlib.redirect_stdout, and also stderr with contextlib.redirect_stderr.

import os
import contextlib

with open(os.devnull, "w") as f, contextlib.redirect_stdout(f):
    print("This won't be printed.")
Answered By: ForgottenUmbrella

If you want to block print calls made by a particular function, there is a neater solution using decorators. Define the following decorator:

# decorater used to block function printing to the console
def blockPrinting(func):
    def func_wrapper(*args, **kwargs):
        # block all printing to the console
        sys.stdout = open(os.devnull, 'w')
        # call the method in question
        value = func(*args, **kwargs)
        # enable all printing to the console
        sys.stdout = sys.__stdout__
        # pass the return value of the method back
        return value

    return func_wrapper

Then just place @blockPrinting before any function. For example:

# This will print
def helloWorld():
    print("Hello World!")

# This will not print
def helloWorld2():
    print("Hello World!")
Answered By: Fowler

Based on @Alexander Chzhen solution, I present here the way to apply it on a function with an option to suppress printing or not.

    import os, sys
    class SuppressPrints:
        #different from Alexander`s answer
        def __init__(self, suppress=True):
            self.suppress = suppress

        def __enter__(self):
            if self.suppress:
                self._original_stdout = sys.stdout
                sys.stdout = open(os.devnull, 'w')

        def __exit__(self, exc_type, exc_val, exc_tb):
            if self.suppress:
                sys.stdout = self._original_stdout
    def foo(suppress=True):
        with SuppressPrints(suppress):
            print("It will be printed, or not")

    foo(True)  #it will not be printed
    foo(False) #it will be printed

I hope I can add my solution below answer of Alexander as a comment, but I don`t have enough (50) reputations to do so.

Answered By: Quan Hoang

You can do a simple redirection, this seems a lot safer than messing with stdout, and doesn’t pull in any additional libraries.

enable_print  = print
disable_print = lambda *x, **y: None

print = disable_print
function_that_has_print_in_it(1)  # nothing is printed

print = enable_print
function_that_has_print_in_it(2)  # printing works again!

Note: this only works to disable the print() function, and would not disable all output if you’re making calls to something else that is producing output. For instance if you were calling a C library that was producing it’s own output to stdout, or if you were using intput().

Answered By: xelf

If you are using Jupyter Notebook or Colab use this:

from IPython.utils import io

with io.capture_output() as captured:
    print("I will not be printed.")
Answered By: PascalIv
"stop a function from calling print"
# import builtins
# import __builtin__ # python2, not test
printenabled = False
def decorator(func):
    def new_func(*args,**kwargs):
        if printenabled:
    return new_func
print = decorator(print) # current file
# builtins.print = decorator(builtins.print)  # all files
# __builtin__.print = decorator(__builtin__.print) # python2

import sys
import xxxxx
def main():
    global printenabled
    printenabled = True
    print("1 True");
    printenabled = False
    print("2 False");
    printenabled = True
    print("3 True");
    printenabled = False
    print("4 False");
if __name__ == '__main__':

print: 1 True
print: 3 True

Answered By: user17527081

Change value of file object of print() function. By default it’s sys.stdout, instead we can write to null device by open(os.devnull, 'w')

import os, sys

mode = 'debug' #'prod'

if mode == 'debug':
    fileobj = sys.stdout
    fileobj = open(os.devnull,'w')

print('Hello Stackoverflow', file = fileobj)
Answered By: Shubham Kumar Singh

If you want to enable/disable print with a variable, you could call an auxiliary function instead print, something like printe(the name is just for convenience)

def printe(*what_to_print):
if prints_enable:
    string = ""
    for items in what_to_print:
        string += str(items) + " "
Answered By: jcbordon96

Define a new Print function where you enable print first. print your output next. And then disable print again.

def Print (*output):
   print (output)

with one of the above "safe" enable / disable pair of function

Answered By: user2800464
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.