Mute printing of an imported Python script
<p>I want to import a python script in to another script. </p>

<pre><code>$ cat py1.py
test=("hi", "hello")

print test[0]

$ cat py2.py
from py1 import test

print test
</code></pre>

<p>If I execute <code>py2.py</code>:</p>

<pre><code>$ python py2.py 
hi
('hi', 'hello')
</code></pre>

<p>Can I anyway mute the first <code>print</code> which is coming from the <code>from py1 import test</code>?</p>

<p>I can't comment the <code>print</code> in <code>py1</code>, since it is being used somewhere else. </p>

<p>Simply open the /dev/null device and overwrite the <code>sys.stdout</code> variable to that value when you need it to be quiet.</p>

<pre><code>import os
import sys

old_stdout = sys.stdout
sys.stdout = open(os.devnull, "w")

from py1 import test

sys.stdout = old_stdout
print test
</code></pre>

python