I am new to iPython and trying to help another developer get started and we are both hitting same issues.
We are attempting to run a python unittest sample in iPython from https://docs.python.org/2/library/unittest.html#basic-example The code runs just fine from command line on windows and ubuntu without ANY modifications Exact same code from iPy notebook generates following exception:
AttributeError: 'module' object has no attribute '/home/myuser/'
The filename is: /home/myuser/example_unittest.ipynb
I have noodled the iPython docs and google with no luck as of the moment. Any debugging tips, or clues to solving this issue are appreciated.
(full stack):
AttributeError Traceback (most recent call last)
<ipython-input-2-39bc0ec16f11> in <module>()
28
29 if __name__ == '__main__':
---> 30 unittest.main()
31
/usr/lib/python2.7/unittest/main.pyc in __init__(self, module, defaultTest, argv, testRunner, testLoader, exit, verbosity, failfast, catchbreak, buffer)
92 self.testLoader = testLoader
93 self.progName = os.path.basename(argv[0])
---> 94 self.parseArgs(argv)
95 self.runTests()
96
/usr/lib/python2.7/unittest/main.pyc in parseArgs(self, argv)
147 else:
148 self.testNames = (self.defaultTest,)
--> 149 self.createTests()
150 except getopt.error, msg:
151 self.usageExit(msg)
/usr/lib/python2.7/unittest/main.pyc in createTests(self)
156 else:
157 self.test = self.testLoader.loadTestsFromNames(self.testNames,
--> 158 self.module)
159
160 def _do_discovery(self, argv, Loader=loader.TestLoader):
/usr/lib/python2.7/unittest/loader.pyc in loadTestsFromNames(self, names, module)
126 of string specifiers. See 'loadTestsFromName()'.
127 """
--> 128 suites = [self.loadTestsFromName(name, module) for name in names]
129 return self.suiteClass(suites)
130
/usr/lib/python2.7/unittest/loader.pyc in loadTestsFromName(self, name, module)
98 obj = module
99 for part in parts:
--> 100 parent, obj = obj, getattr(obj, part)
101
102 if isinstance(obj, types.ModuleType):
AttributeError: 'module' object has no attribute '/home/myuser/'
unittest.main()
is primarily for command line execution.
In order to run a unittest in the ipython notebook, remove the if __name__ == '__main__'
part of the code and, in a new cell, create a test suite and then run it using TextTestRunner
,
suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)
unittest.TextTestRunner().run(suite)
The reason you are getting that error because of unittest.main checks for arguments (sys.argv). It's what starts iPython or Jupyter.
Change your code to:
if __name__ == '__main__':
unittest.main(argv=['first-arg-is-ignored'], exit=False)
In the notebook, you will also want to include exit=False
to prevent unittest.main from trying to shutdown the kernel process:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With