Websphere’s Implementation of Jython (wsadmin) Broke __name__

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__':

you have to write it differently with WebSphere Jython like this:

if __name__ == 'main':

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':

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__':

write it this way with Websphere Jython:

if __name__ == '__main__' or __name__ == 'main':
    suite = unittest.TestSuite()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s