How to execute file in Python with arguments using import?
<p>I read that you can execute a file using <code>import</code> like this<br>
<strong>file.py</strong>:</p>

<pre><code>#!/usr/bin/env python
import file2
</code></pre>

<p><strong>file2.py</strong>:</p>

<pre><code>#!/usr/bin/env python
print "Hello World!"
</code></pre>

<p>And <strong>file.py</strong> will print <code>Hello World</code>. How would I execute this file with arguments, using <code>import</code>?</p>

<p>Import is not meant for execution of scripts. It is used in order to fetch or "import" functions, classes and attributes it contains.</p>

<p>If you wish to execute the script using a different interpreter, and give it arguments, you should use <a href="https://docs.python.org/3/library/subprocess.html#using-the-subprocess-module" rel="nofollow"><code>subprocess.run()</code></a>.</p>

<hr>

<p>In Python 2 you may use <a href="https://docs.python.org/2.7/library/subprocess.html#subprocess.call" rel="nofollow"><code>subprocess.call()</code></a> or <a href="https://docs.python.org/2.7/library/subprocess.html#subprocess.check_output" rel="nofollow"><code>subprocess.check_output()</code></a> if you need the programs output.</p>

python python-2.7 python-import