First time here? We are a friendly community of Power Systems Engineers. Check out the FAQ!

Ask Your Question
2

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

asked Apr 19 '12

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)

1 answer

Sort by » oldest newest most voted
2

answered Apr 20 '12

Daniel_Hillier gravatar image

updated Apr 22 '12

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.
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 gravatar imagechip (Apr 20 '12)

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.
Want to format code in your answer? Here is a one minute demo on Youtube

Add Answer

[hide preview]

Question Tools

Stats

Asked: Apr 19 '12

Seen: 1,089 times

Last updated: Apr 21 '12