Websphere’s implementation of Jython in wsadmin broke sys.argv

In CPython and JPython, the first index of sys.argv points to the name of the Python script, while the rest of the array elements refer to the arguments. However, in Websphere’s Jython, the first index already points to the first argument of the script. This means that none of the elements of sys.argv has the information about the name of the script it is running.

I think this is a serious drawback. I have some functionalities where I need access to the name of the script. For example, I need to know the directory where that script is located. This will allow me to navigate relatively to other files where the script is located like additional path to libraries (pythonpath ) or configuration files and be able to access them at runtime.

I have yet to figure out a trivial way to access the script name at runtime in wsadmin. One workaround I am planning is to write a wrapper script to grab the script name and then pass that information to wsadmin’s Jython script as the first parameter, so that would become sys.argv[0]. Or perhaps as an option parameter with the value telling it that is the script name.

However, the problem with a wrapper script is that your wsadmin Jython scripts won’t be portable since it would be dependent on the wrapper to run.

If anyone out there has ideas other than a wrapper script, I’d love to hear it.

7 thoughts on “Websphere’s implementation of Jython in wsadmin broke sys.argv

  1. Just came across your article while trying to find out how to do this myself. After failing to find anyone who’s done this, here’s how I’ve implemented it. The command line is available as a Java envvar, and I just pull the -f option out of it:

    import java.lang.System

    cmdLine = System.getenv(“IBM_JAVA_COMMAND_LINE”)
    scriptName = re.sub(“.*-f ([^ ]*).*”, r”\1″, cmdLine)

  2. I know this is probably dead, but as it was one of the only useful sites I found when attempting to resolve my issue, I thought I would post a code snippet that I found useful for assisting with importing files:

    import os
    import os.environ
    import re

    cmdLine = os.environ.get(“IBM_JAVA_COMMAND_LINE”,”)
    scriptName = re.sub(“.*-f ([^ ]*).*”, r”\1″, cmdLine)
    scriptPath = os.path.dirname(scriptName)

    print scriptName
    print scriptPath

    if scriptPath and os.path.isfile(scriptPath+”/utils.py”):
    #execfile(scriptPath+”/utils.py”)
    exec(open(scriptPath+”/utils.py”,”rb”).read())

  3. Pingback: WebSphere Jython scripting, sys.argv[0] and __file__ « Dave Brand's Blog

Leave a reply to Dani Cancel reply