Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

suppress output on library import in python

Tags:

python

import

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

like image 783
jorgehumberto Avatar asked Jun 09 '26 08:06

jorgehumberto


1 Answers

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__
like image 73
Green Cloak Guy Avatar answered Jun 10 '26 22:06

Green Cloak Guy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!