In Websphere Jython (as of WAS6.1), the administrative objects are not automatically accessible from imported modules, unlike when you call them from your main script. It is also not possible to import them either since they are not defined in sys.modules.
For example, the code above will break if this Jython module is imported from the main script or another module. If this is executed as the main script it would work right off the bat even without importing anything.
To fix this problem, you need to add the administrative objects to the “sys.modules” dictionary in the main script so that they will be visible to all imported modules. Once the administrative objects are available in “sys.modules”, your imported modules can then call the import command for those object to access them.
For example, in your main script, you need to construct the lines shown below before you can import any modules that will require access to the WebSphere administrative objects:
import sys sys.modules['AdminConfig'] = AdminConfig sys.modules['AdminControl'] = AdminControl sys.modules['AdminApp'] = AdminApp sys.modules['AdminTask'] = AdminTask sys.modules['Help'] = Help # now you can import your module import myModule
Your imported modules can then call the import command to access these objects. The module below shows import commands for each WebSphere Administrative object.
try: import AdminConfig import AdminControl import AdminApp import AdminTask import Help except: pass # this works now even if this module is imported print AdminConfig.types()
The try-except clause above is used to prevent the module from failing if you tried to run this as your main script. If you tried to run this in your main wsadmin script and the sys.modules were not set, this would simply ignore the exceptions and should be able to proceed to run any administrative commands directly.
I hope someday IBM will fix this problem by simply adding the administrative objects to the sys.modules by default so that we won’t need to do this ourselves.