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>You might want to consider changing the other script to still print when its run 'in the other place' - if you're running py1 as a shell command, try to make sure all "executable statements" in a file are inside a block.</p>

<pre><code>if __name__ == "__main__":
    print test
</code></pre>

<p>(see <a href="http://stackoverflow.com/questions/419163/what-does-if-name-main-do">What does `if __name__ == &quot;__main__&quot;:` do?</a>)</p>

<p>This would fix the underlying issue without having you do weird things (which would be redirecting the standard out, and then putting it back etc), or opening the file and executing line by line on an if block.</p>

python