Is there a way to use the python -m mymod syntax from within the python interpreter?
<p>Many packages like unittest have an easy to use command line interface, e.g. the test discovery feature in unittest: <a href="https://docs.python.org/2/library/unittest.html#test-discovery" rel="nofollow">https://docs.python.org/2/library/unittest.html#test-discovery</a></p>

<p>However, to achieve the same from within python, it is sometimes necessary to dig in much deeper in the documentation. In the example above, the python code required to achieve the same is much harder to figure out compared to the command line command. </p>

<p>Therefore, I want to know: Is there a consistent way to translate <code>python -m mymod args</code> to something, that can be used within the python interpreter?</p>

<p>Edit: I'm asking for a reasonable strategy what to do in a situation, where I know the python -m command but nothing more. Is this knowledge completely useless when I am forced to use the python interpreter?</p>

<p>You can use the <a href="https://docs.python.org/2/library/runpy.html#runpy.run_module" rel="nofollow"><code>runpy.run_module</code> function</a>:</p>

<pre><code>import runpy
import sys

sys.argv[1:] = ['arg1', 'arg2', 'arg3']
runpy.run_module('module.name', run_name='__main__', alter_sys=True)
</code></pre>

<p>This executes <code>module.name</code> as if you typed:</p>

<pre><code>python -m module.name arg1 arg2 arg3
</code></pre>

python