First time here? Check out the FAQ!
2

How to detect if Python or PSSE is running the show.

I have a script that is normally launched from the shell with PSSE on top like so:

psse32 -buses 150000 -pyfile "MyScript.py" -embed

I am converting the script to run with Python on top as part of a larger python program. Does anyone know how to detect if I am running as an embedded session in PSSE or standalone?

I found have an indirect route by explicitly setting the mode:

def main(embedded=True):
    ...
    if embedded:
        psspy.stop_2()
    else:
        # do something else to finish up.

if __name__ == '__main__':
    # PSSE embedded scripts enter here 
    main()

And invoke the script using:

import MyScript
MyScript.main(embedded=False)
chip's avatar
459
chip
asked 2012-04-19 10:30:49 -0500
edit flag offensive 0 remove flag close merge delete

Comments

add a comment see more comments

1 Answer

2

You can see which application started Python by looking at sys.executable, which returns the executable that was used to start the Python session.

import os
import sys

def main():
    # Determine if this script is being run from PSSE or plain Python.
    full_path_executable = sys.executable
    # Remove the folder path and keep only the executable file (in lower case).
    executable = os.path.basename(full_path_executable).lower()

    run_in_psse = True
    if executable in ['python.exe', 'pythonw.exe']:
        # If the executable was one of the above, it is a Python session.
        run_in_psse = False        

    if not run_in_psse:
        psspy.stop_2()
    else:
        # do something else to finish up.
Daniel_Hillier's avatar
462
Daniel_Hillier
answered 2012-04-19 20:15:12 -0500, updated 2012-04-21 19:53:53 -0500
edit flag offensive 0 remove flag delete link

Comments

Looks like a nice trick, the sys.executable [docs](http://docs.python.org/library/sys.html#sys.executable) in Python don't really explain this.

chip's avatar chip (2012-04-19 22:19:20 -0500) edit
add a comment see more comments