Ask Your Question
0

[SOLVED] Import custom Python file

asked 2022-06-28 11:41:59 -0500

miltonfrey gravatar image

updated 2022-06-29 14:16:48 -0500

I am writing lots of scripts and keep using the same functions in each script so I wanted to write a separate boilerplate Python file to store those functions and import them into my main file. This works perfectly when I am running the script outside of PSSE, but breaks when I run it in PSSE. It gives me the error NameError: global name 'psspy' is not defined. I know this error is in the boilerplate file and not the main file because the main file runs psspy.case successfully.

I have tried all four combinations of importing and not importing psspy in the main and boilerplate file with the code:

PSSE_LOCATION = os.path.join('C:', os.sep, 'Program Files (x86)', 'PTI', 'PSSE34', 'PSSPY27')
sys.path.append(PSSE_LOCATION)
os.environ['PATH'] = os.environ['PATH'] + PSSE_LOCATION + ';'
import psspy

I have viewed the question "How to import a custom Python module in PSSE?" (I can't link it as I don't have enough karma), however, their problem was the module not importing correctly. The boilerplate module imports just fine, but it can't access psspy when running in PSSE.

Am I going about importing custom Python files incorrectly?

I am running PSSE 34 and Python 2.7.

edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted
1

answered 2022-06-30 01:00:14 -0500

perolofl gravatar image

Use reload instead of closing and reopen PSSE after changes in the custom file imported by the main script.

Example:

import customfile
reload(customfile)

Now, the updated customfile will be imported again inside PSSE.

In Python 3:

import customfile
from importlib import reload
reload(customfile)
edit flag offensive delete link more

Comments

Thanks, this worked perfectly. Still need to import psspy, but this makes it much easier.

miltonfrey gravatar imagemiltonfrey ( 2022-06-30 07:30:19 -0500 )edit
0

answered 2022-06-29 14:15:38 -0500

miltonfrey gravatar image

updated 2022-06-29 15:50:57 -0500

Okay, I solved it. The boilerplate code didn't recompile until I closed PSSE which is why my error changed today. The problem I was having yesterday is that the boilerplate code needed to import psspy for some reason, even though it was running in PSSE (GUI). The code for importing is:

import sys, os
PSSE_LOCATION = os.path.join('C:', os.sep, 'Program Files (x86)', 'PTI', 'PSSE34','PSSPY27')
sys.path.append(PSSE_LOCATION)
os.environ['PATH'] = os.environ['PATH'] + PSSE_LOCATION +';'
import psspy from psspy import _i,_f, _s

The code would not run without this importing. The main code did not need to import it, but the boilerplate did.

So, importing a custom Python file works just fine, just make sure to import psspy in the custom file and close and reopen PSSE after changing code in the custom file so it can recompile.

EDIT

After reading through the docs some more, an easier version of the code above is:

import psse34
import psspy
from psspy import _i, _f, _s
edit flag offensive delete link more
0

answered 2022-06-29 13:12:21 -0500

jconto gravatar image

When running in PSSe (GUI), there is no need to import psspy. It is loaded automatically when PSSe start up. PSSe changes the working directory to the folder where the python script is located. This action could mess the relative path to access other python scripts from the main script. If absolute paths are used (full path starting from c:), importing or loading files should work ok.

Print the "sys.path" before the error occurs in both cases (in GUI or outside of it), looking for differences or missing paths.

The posted code looks ok to me (btw, I don't see the need to update "os.environ['PATH']"). You can post enough of your code (main + boilerplate code) to check the problem happening.

edit flag offensive delete link more

Comments

So, my old error has gone away, I'm not sure what I changed, but you are probably right with printing "sys.path" to see if there is an error. The path did include my working directory, I wish I could have done that yesterday. Now it's saying "_i" is not defined, I'll update the post.

miltonfrey gravatar imagemiltonfrey ( 2022-06-29 13:46:46 -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

1 follower

Stats

Asked: 2022-06-28 11:41:59 -0500

Seen: 831 times

Last updated: Jun 30 '22