Products Resources Support About Us

Detect the UV context that launched the python code


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


In a python module named “” 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:


then I get this output:

    Arguments = 2
    Argument List = ['uv', '/UVdata/disk1/ERA/POC_PP/']

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


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.


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