Python 3: How to call function from another file and pass arguments to that function ?
<p>I want to call a function from another file and pass arguments from current file to that file. With below example, In file <strong>bye.py</strong> I want to call function "<strong>me</strong>" from "<strong>hi.py</strong>" file and pass "<strong>goodbye</strong>" string to function "<strong>me</strong>". How to do that ? Thank you :)</p>

<p>I have file hi.py</p>

<pre><code>def me(string):
    print(string)
me('hello')
</code></pre>

<p>bye.py</p>

<pre><code>from hi import me
me('goodbye')
</code></pre>

<p>What I got:</p>

<pre><code>hello
goodbye
</code></pre>

<p>What I would like: </p>

<pre><code>goodbye
</code></pre>

<p>Generally when you create files to be imported you must use <code>if __name__ == '__main__'</code> which evaluates to false in case you are importing the file from another file. So your hi.py may look as:</p>

<pre><code>def me(string):
    print(string)

if __name__ == '__main__':
    # Do some local work which should not be reflected while importing this file to another module.
    me('hello')
</code></pre>

python function python-3.x