Python for Power Systems

A blog for power systems engineers to learn Python.

Run PSSE From Python and Not the Other Way Around

What this, the Slashie, mean is you consider me the best actor slash model and not the other way around.

Fabio Zoolander

How do you run your Python scripts on PSSE? Do you open the PSSE program and run from the “Run Program Automation File..” menu? I know some of my colleagues have created macro buttons that sit on their customised toolbar. The macro button is linked to a Python file and when you click the button that Python file runs.

Both of these exemplify the case where PSSE is run first, and then your Python script is executed. PSSE is the boss and your Python script is the worker that is told when to run.

I’d like to take you through another way to run your Python scripts. The examples I’ll show you will enable your Python script to be the boss and for it to tell PSSE to run.

Why would I care who is the boss?

Many times, PSSE is just one small cog in your overall program. You need it to do all the heavy computations for your latest energy congestion forecast but you still want to send the results by email to your boss in Singapore, or publish your results on the interwebs (Yes you can do that, I’ll explain how in a later post). Then consider testing, debugging, calling your script from another program.

There are lots of cases where it absolutely makes sense to have Python firmly seated on the driver’s side with two hands on the steering wheel and its foot pushing the accelerator through the floor. PSSE is a great tool and running your programs from inside it is fine for some, but sometimes you need to let your Python scripts run free.

Show me the code

Ok, you asked and I’m not one to hold out on you. Here is some code that is suggested in the PSSE manual to get an External Interpreter Environment. I’ll take you through what it does, so you can see through the magic. I will suggest methods that will make including this in your own work a piece of cake.

First Python is its own language, complete with a full library of useful tools. The only way Python knows to find those useful tools is to look in every single directory listed in its path variable. Python does not know about PSSE at all. You need to tell it where to look to find the PSSE library files (psspy, redirect et al).

The first parts of the program, beginning with sys.path.append and os.environ['PATH'] are adding to the Python and system path variables. Only now does Python know where to look to find psspy. If you don’t believe me, try running the line import psspy from your Python shell (not inside PSSE). You should get an ImportError. Now tell Python where it can find psspy using the first three lines in the example and try import psspy again.. No error!

There are a couple of important other lines in the code before we are done, the rest are not helpful.

If you want output that PSSE would normally print while it is running to show up on the command line for your Python script use this

import redirect

Those lines are a matter of taste, your program won’t break without them.

import psspy

This was the whole point of the exercise, to get this psspy library. That is where the interaction with PSSE occurs. So here is the minimum that you need:

A word of warning about os.chdir

The best place for these commands is near the top of your Python script file or in another file in the same directory as your Python script file which you import. No one expects for a library they are importing to change the working directory, especially not to c:\work_dir so don’t use os.chdir.

Putting it all together

Here is a basic script that will open up the save case of your choice and run a fully coupled Newton-Raphson iterative solve. Just a starting point skeleton file to give you an idea of how all of this sits together:


Very helpful! thank you


this is brilliant!