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>py1.py use an <code>if __name__=="__main__":</code></p>

<p>So like your py1.py would look like: </p>

<pre><code>def main():
    test=("hi", "hello")

    print test[0]

if __name__=="__main__":
    main()  
</code></pre>

<p>This will allow you to still use py1.py normally, but when you import it, it won't run the <code>main()</code> function unless you call it.</p>

<p><a href="https://docs.python.org/3/library/__main__.html" rel="nofollow">This explains what's going on</a></p>

python