Products Resources Support About Us

Detect the UV context that launched the python code


#1

Is it possible from within python code to determine how the code was launched from within UniVerse?

There appear to be 3 main options for initiating the execution of python code

  1. Launch a python module using the RUNPY command
  2. Launch the PYTHON command to access the python shell
  3. Use the BASIC API functions: PyCall, PyCallFunction, PyCallMethod

(Not sure why there i no VOC entry, similar to the “VR” type for basic programs, that launches a python program - opportunity missed, I think).

I want to be able to determine how the python code was launched so am wondering if there is any pythonic approach to establish this


#2

In a python module named “test-context.py” I have the following logic:

if __name__ == '__main__':
    if len(sys.argv):
        print("Arguments = {}".format(len(sys.argv)))
        print("Argument List = {}".format(str(sys.argv)))

This should output the arguments passed to the python code.

If I then run the code using the following TCL command:

RUNPY PP test_context.py FRED 123

then I get this output:

RUNPY POC_PP test_context.py FRED
    Arguments = 2
    Argument List = ['uv', '/UVdata/disk1/ERA/POC_PP/test_context.py']

so this gives us one clue as to how this module was launched.


#3

After some experimenting, it turns that that when you use the PyCallFunction() API call in BASIC then the sys.argv attribute does not exist!

So now we have 2 known indicators of context:

  1. No sys.argv means it is an API call.
  2. If sys.argv[0] == 'uv' it is a RUNPY execution.

#4

Interesting topic, I will have to look a bit deeper, but have you looked at sys.executable?