The 'module' object does not have the 'py' attribute when launched from cmd

I am currently studying unit testing, and I came across a strange error:

If I run my script from PyCharm, everything works fine. If I run it from my own cmd.exe(as an administrator), I get the following error:

enter image description here

This is my 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'
+13
source share
2 answers

Just uninstall the extension .py.

You run your tests using the command line flag -m. The Python documentation will tell you more about this, just check out this link .

, -m , - unittest. , Python ( ). , FirstTest mytests mypackage, :

python -m unittest mypackage.mytests.FirstTest

, mypackage. , ( ).

.py, unittest py (, ) , , . , :

AttributeError: ’module’ object has no attribute ’py’
+31

:

if __name__ == "__main__":
    unittest.main()

python test_my_function.py

0

Source: https://habr.com/ru/post/1654782/


All Articles