Ask Your Question
2

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

asked 2012-04-19 10:30:49 -0600

chip gravatar image

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)
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2012-04-19 20:15:12 -0600

Daniel_Hillier gravatar image

updated 2012-04-21 19:53:53 -0600

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.
edit flag offensive delete link more

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 gravatar imagechip ( 2012-04-19 22:19:20 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

[hide preview]

Question Tools

Stats

Asked: 2012-04-19 10:30:49 -0600

Seen: 1,080 times

Last updated: Apr 21 '12