Using local Python modules from python -c '...'
<p>I'm having trouble getting the Python 3.5 CLI to run commands with the <code>-c</code> switch.</p>

<p>If I try <code>python3 -c "print(5 + 9)"</code> in the shell, the output is <code>14</code> as expected.</p>

<p>However, I have a file in the current working directory called <code>gcd.py</code> that looks like this:</p>

<pre><code>def gcd(m, n):
    return m if n == 0 else gcd(n, m % n)
</code></pre>

<p>If I run <code>python3 -m gcd -c "print(gcd(48, 18))"</code> the shell just creates a new command prompt without outputting anything.</p>

<p>If I change the file to:</p>

<pre><code>print('test')

def gcd(m, n):
    return m if n == 0 else gcd(n, m % n)
</code></pre>

<p>then the shell will output <code>test</code>, so the file is being loaded. Does anybody know what I'm doing wrong? Thanks.</p>

<p>You can't use <code>-m</code> and <code>-c</code> together; either one or the other controls execution. Specifying them both makes Python ignore them.</p>

<p>Use an <code>import</code> statement in <code>-c</code> instead:</p>

<pre><code>python3 -c "from gcd import gcd; print(gcd(48, 18))"
</code></pre>

<p>Note that <code>-m</code> treats the module as a script, it is not a 'import this module first' switch for <code>-c</code>.</p>

python shell