Ask Your Question
4

How do I import the psspy module in a Python script?

asked 2012-01-04 02:28:37 -0500

Daniel_Hillier gravatar image

updated 2012-01-12 05:32:14 -0500

JervisW gravatar image

I've been running PSSE from the Graphical interface for a while now, but a colleague told me I could automate some tasks by using Python to run the show. I'm familiar with Python and I've tried importing the psspy module by issuing,

import psspy

at the start of the script but I am getting this error:

Traceback (most recent call last):
  File "qv.py", line 1, in <module>
    import psspy
ImportError: No module named psspy

What am I missing?

edit retag flag offensive close merge delete

5 answers

Sort by » oldest newest most voted
3

answered 2012-01-05 20:49:06 -0500

JervisW gravatar image

Hi Daniel,

It looks like Python isn't able to find your psspy.py module.

Python looks for files to import in all of the directories listed in sys.path. If you want to look at the directories that are listed by default - type this into your interpreter:

import sys
print sys.path

The path is just a Python list, which means we can add new directories at the end of it. Assuming that you have installed PSSE in the default location lets add the PSSE directory so Python can find the psspy module:

import os,sys

PSSE_LOCATION = r"C:\Program Files\PTI\PSSE32\PSSBIN"
sys.path.append(PSSE_LOCATION)
os.environ['PATH'] = os.environ['PATH'] + ';' +  PSSE_LOCATION 

import psspy

PSSE itself requires that you add to the PATH environment variable so it can find it's own files. We do that by adding to os.environ['PATH'] with a ';' semi-colon separator.

Make sure you always write that second block of code before you import psspy. Now that you have added the correct directories to your path, you can import psspy from within your Python script like a pro.

edit flag offensive delete link more

Comments

1

Thanks for the code. That fixed my problem.

Daniel_Hillier gravatar imageDaniel_Hillier ( 2012-01-05 20:53:07 -0500 )edit
6

answered 2012-01-12 17:17:14 -0500

chip gravatar image

updated 2012-01-16 11:57:01 -0500

The method proposed by JervisW requires a chuck of code to be pasted at the top of every script. This would also be a small inconvenience when we begin moving to PSSE33 and in the unlikely chance that someone had PSSE installed in a different path.

Adding PSSE modules python search path

You can modify the python search path by adding a .pth file to the site-packages directory which looks like:

C:\Python25\Lib\site-packages
$ cat .\pti.pth
C:\Program Files\PTI\PSSE32\PSSBIN

And a quick check to make sure python can find the psspy module:

 python -m psspy

If python can't find the module you'll get a traceback.

Adding PSSBIN to the system PATH

To work properly, PSSEBIN needs to be in the system path. Otherwise you get insightful error messages when you try to load a case like:

Invalid file type. example2.sav (OpnApplFil/OPNPTI)

In my opinion, modifying the system PATH within python just feels wrong. I would prefer to add the PSSEBIN to you path manually.

Automagically Adding PSSBIN to the system PATH

TODO: This method only seems to work if you invoke python for interactively. To actually work whit will have to be modified.

PSSE docs seems to imply that adding PSSBIN to the system PATH is a good idea anyways.

Honestly, most users don't know what an environment variable is, so there's probably no harm in modifying it on the fly. Using the installed package mindset we can put the code in an init.py file in our own directory in site-packages.

C:\Python25\Lib\site-packages
$ cat .\pti\__init__.py
import os
import sys

PSSE_LOCATION = r"C:\Program Files\PTI\PSSE32\PSSBIN"
os.environ['PATH'] = os.environ['PATH'] + os.pathsep + PSSE_LOCATION

Now when we start python we have the modified path:

>>> os.environ['PATH'].split(os.pathsep)[-1]
'C:\\Program Files\\PTI\\PSSE32\\PSSBIN'

Intelligently Finding PSSE Install

You should be able to identify the location of PSSBIN reliably through an environment variable or a windows registry entry. This would make the psse-python integration work anywhere.

However, I can't find anything useful in the environment or registry. Maybe it's buried somewhere under Documents and Settings.

Alternate: Using .bat file to setup environment

PSSE sets the example of using batch files to setup your PSSE environment. See Start Menu -> PSSE-32 -> PSSE-32 Command Prompt. We can follow this example and modify the PSSE-32 Command Prompt batch file like this:

@echo OFF
REM == Start Sane Python Setup ==
SET PYTHONPATH=C:\PROGRA~1\PTI\PSSE32\PSSEBIN;%PYTHONPATH% 
SET PATH=C:\Python25\;%PATH%
REM == End Sane Python Setup ==          
PATH=C:\PROGRA~1\PTI\PSSE32\PSSBIN;C:\PROGRA~1\PTI\PSSE32\PSSLIB;%PATH%
echo.
echo Your PATH is now set to run PSS®E-32 programs!
echo.

Before any PSSE scripts are run, users have to open the special PSSE-32 command prompt. I think most of the users already do this, so it may dovetail into existing habits.

Why all this work?

I want ... (more)

edit flag offensive delete link more

Comments

1

Good idea chip. The only thing I'd add is that the operating system PATH variable needs to have the path to PSSBIN added. The PSSE program itself relies on finding PSSBIN. Is there a way to put additional code into the .pth file?

JervisW gravatar imageJervisW ( 2012-01-12 22:15:40 -0500 )edit
1

Chip, really impressed with this further developed idea. I think you are really hitting on a sticking point that many people have had, but it has not really been addressed adequately so far. How about you and I set up your idea psse-python-integration package?

JervisW gravatar imageJervisW ( 2012-01-14 04:10:37 -0500 )edit
1

@JervisW Absolutely. I'd like get something setup that makes our environment sane. I still haven't figured out what that is yet, but this is going to be a growing problem for us. We also have the Python25 vs Python26 magic number bit to sort out.

chip gravatar imagechip ( 2012-01-16 10:54:45 -0500 )edit

@chip This 'sane' setup does fit with the natural PSSE workflow that I've seen. Does PTI provides a similar batch file as part of their Python PSSE environment manager? Would you be happy to host this batch file as a gist or a project on github?

jtrain gravatar imagejtrain ( 2012-01-16 19:03:29 -0500 )edit

@chip github hosts public projects like this free of charge, and a large part of the Python community regularly use it. We could link to it from here as the canonical way for people to set their environment.

jtrain gravatar imagejtrain ( 2012-01-16 19:06:16 -0500 )edit
0

answered 2016-08-01 16:15:29 -0500

lleb gravatar image

this is more of a follow up question: I am getting the following error with your code:

 Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 
====================== RESTART: C:\Python27\program1.py ======================

Traceback (most recent call last):
  File "C:\Python27\program1.py", line 7, in <module>
    import psspy
ImportError: No module named psspy

I copied and pasted your code and only added 1 single line and adjusted the path:

import os,sys

PSSE_LOCATION = r"C:\Program Files (x86)\PTI\PSSEXplore34\PSSBIN"
sys.path.append(PSSE_LOCATION)
os.environ['PATH'] = os.environ['PATH'] + ';' +  PSSE_LOCATION 

import psspy

psspy.psseinit(50)

What am I missing? FYI I do not have a psspy.py in that directory, but do have psspy.pyc and psspyc.pvd in: C:\Program Files (x86)\PTI\PSSEXplore34\PSSPY27

When I run with that path i get the following error:

====================== RESTART: C:\Python27\program1.py ======================

Traceback (most recent call last):
  File "C:\Python27\program1.py", line 7, in <module>
    import psspy
  File ".\psspy.py", line 56, in <module>
ImportError: DLL load failed: The specified module could not be found.

I find both rather confusing and offering little in the way of help with their error messages. Am I missing a DLL file or more, or is psspy just no longer provided with PSSEXplore?

edit flag offensive delete link more

Comments

I ran into the same trouble too. Have you solved this problem and could you share the solution? Thank you a lot!

Cynthia Hsu gravatar imageCynthia Hsu ( 2017-03-13 07:03:08 -0500 )edit
0

answered 2016-08-01 20:09:22 -0500

jconto gravatar image

If you use v.34, check the post "psspy module not found in PSS/E 34"

edit flag offensive delete link more
0

answered 2012-05-02 13:29:35 -0500

Fagundes gravatar image

updated 2012-05-06 23:39:23 -0500

jtrain gravatar image

Answer has been migrated to become its own question http://forum.whit.com.au/psse-help-forum/question/265/python-23-on-psse-30

Question was relating to running PSSE v 30 with Python 2.3. Importing psspy caused a missing dll error.

edit flag offensive delete link more

Comments

  • Can you see a file called psspy.pyc
  • What happens if you run this file from inside PSSE?
jtrain gravatar imagejtrain ( 2012-05-03 04:22:11 -0500 )edit

No, there is not that file in PSSBIN paste. I have the student version of PSS/E 32 wich has the psspy.pyc. I tried to copy that file to the PSSBIN of PSSE 30 but the same error occurs. If I run the file from inside PSSE 30 it works properly with no error.

Fagundes gravatar imageFagundes ( 2012-05-03 12:00:50 -0500 )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

2 followers

Stats

Asked: 2012-01-04 02:28:37 -0500

Seen: 28,375 times

Last updated: Aug 01 '16