suppress output on library import in python
Asked Answered
S

3

7

I have a library that I need to import on my code. However, whenever it is imported it outputs several lines of data to the console. How can I suppress the output?

Thanks

Streptokinase answered 20/2, 2020 at 16:33 Comment(3)
Can you fix the library?Endamage
I'd rather not touch the library, as I need to just suppress the output on one particular script and the library is used by several users on the "my" server. The output is just a few lines of text with copyright stuff, so doesn't bother anyone. In this specific case, I need the output to be gone as I am combining bash and python.Streptokinase
ehehe, nothing illegal and the author works with me and doesn't mind. But it's academia, so always good to give credit to the whom the credit is due. But in this particular case, I need a "clean" output to pass to bash (and avoid temp files...)Streptokinase
E
11
import os
import sys

# silence command-line output temporarily
sys.stdout, sys.stderr = os.devnull, os.devnull

# import the desired library
import library

# unsilence command-line output
sys.stdout, sys.stderr = sys.__stdout__, sys.__stderr__
Englishry answered 20/2, 2020 at 16:36 Comment(2)
Very minor point, but I would use sys.stdout = sys.stderr = os.devnull.Emplace
Note quite sure why, but @Emplace suggestions lead to an infinite execution in vscode/jupyter notebook, while the OP did not import sys import os print("hello world") sys.stdout = sys.stderr = os.devnull print("hello world") Stroganoff
R
7

You can try to redirect sys.stdout into a StringIO to capture any text output. So basically everything which would be printed out, will be saved in text_trap.

import io
import sys

#setup text trap
text_trap = io.StringIO()
sys.stdout = text_trap

#to reset the text trap
sys.stdout = sys.__stdout__

A working example:

from io import BytesIO as StringIO
import sys

if __name__ == "__main__":
    print "hello1"

    #setup text trap
    text_trap = StringIO()
    sys.stdout = text_trap

    print("hello2")

    #reset
    sys.stdout = sys.__stdout__
    print "hello3"

Output:

hello1
hello3
Rennold answered 20/2, 2020 at 16:40 Comment(2)
Good one, I prefer the above approach for this particular script, but I'll take note of this in the future. thanks!Streptokinase
On Python 3.9.18, this answer worked for me, while the answer by @Green Cloak Guy did not.Twinkle
W
2

I found this answer and this gist to be really helpful.
E.g. you can just write a small custom context manager to temporarily surpress the
import message of a package

How to suppress the import message:

with suppress_stdout_stderr():
    import library_with_import_message

The contextmanager:

# Via Stack Overflow
# https://mcmap.net/q/325042/-suppress-stdout-stderr-print-from-python-functions
# Via gist https://gist.github.com/vikjam/755930297430091d8d8df70ac89ea9e2
import os
from contextlib import contextmanager, redirect_stderr, redirect_stdout

@contextmanager
def suppress_stdout_stderr():
    """A context manager that redirects stdout and stderr to devnull"""
    with open(os.devnull, 'w') as fnull:
        with redirect_stderr(fnull) as err, redirect_stdout(fnull) as out:
            yield (err, out)
Whitebeam answered 12/6, 2023 at 9:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.