Jython in IBM Websphere does not implement the global variable “__name__” the same way CPython or the official Jython does. Instead of setting it to “__main__” (if the module is not imported), it sets it to “main”.
For example, if you have this code to check if the script is being imported or not,
if __name__ == '__main__': run_method()
you have to write it differently with WebSphere Jython like this:
if __name__ == 'main': run_method()
It’s not a big deal, you’d say. Just check for both values if you are concerned about portability:
if __name__ == '__main__' or __name__ == 'main': run_method()
Unfortunately, that is not the only problem. There are libraries out there that use __main__. One example is unittest.main(). This method doesn’t work with Websphere Jython (WASv61). IBM ships this unittest module with Websphere v6.1, however, the unittest.main() method doesn’t work. Actually, the primary culprit is that Websphere’s Jython doesn’t implement the __main__ module.
As a workaround to this problem, don’t use unittest.main(). Instead, use the methods TestSuite() and TextTestRunner(). For example, instead of the usual way of writing your test class like this:
if __name__ == '__main__': unittest.main()
write it this way with Websphere Jython:
if __name__ == '__main__' or __name__ == 'main': suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(TestMyJythonClass)) unittest.TextTestRunner().run(suite)