'module' object has no attribute 'py' when running from cmd
<p>I'm currently learning unittesting, and I have stumbled upon a strange error:</p>

<p>If I run my script from inside PyCharm, everything works perfectly. If I run it from my <code>cmd.exe</code> (as administrator), I get the following error:</p>

<p><a href="http://i.stack.imgur.com/Q2oDm.png" rel="nofollow"><img src="http://i.stack.imgur.com/Q2oDm.png" alt="enter image description here"></a></p>

<p>This is my code:</p>

<pre><code>import unittest

class TutorialUnittest(unittest.TestCase):
    def test_add(self):
        self.assertEqual(23,23)
        self.assertNotEqual(11,12)

# function for raising errors.
def test_raise(self):
    with self.assertRaises(Exception):
        raise Exception`
</code></pre>

<p>Just remove the <code>.py</code> extension.</p>

<p>You are running your tests using the <code>-m</code> command-line flag. The Python documentation will tell you more about it, just check out this <a href="http://docs.python.org/2/using/cmdline.html#cmdoption-m" rel="nofollow">link</a>.</p>

<p>In a word, the <code>-m</code> option let you run a module, in your case the <code>unittest</code> module. This module expect to receive a module path or a class path following the Python format for module path (using dots). For example, if you want to run the FirstTest class in the mytests module in a mypackage folder you would use the following command line:</p>

<pre><code>python -m unittest mypackage.mytests.FirstTest
</code></pre>

<p>Assuming that you are running the previous command line from the parent folder of mypackage. This allow you to select precisely the tests you want to run (even inside a module).</p>

<p>When you add the <code>.py</code> extension, <code>unittest</code> is looking for a <code>py</code> object (like a module or a class) inside the last element of the module path you gave but, yet this object does not exist. This is exactly what your terminal error tells:</p>

<pre><code>AttributeError: âmoduleâ object has no attribute âpyâ
</code></pre>

python unit-testing cmd