脚本能够通过Python在外部运行一个名为PoiwerFctory的软件,如下所示:
#add powerfactory.pyd path to python path
import sys
sys.path.append("C:\\Program Files\\DIgSILENT\\PowerFactory 2017
SP2\\Python\\3.6")
#import powerfactory module
import powerfactory
#start powerfactory module in unattended mode (engine mode)
app=powerfactory.GetApplication()
#get the user
user=app.GetCurrentUser()
#active project
project=app.ActivateProject('Python Test') #active project "Python Test"
prj=app.GetActiveProject #returns the actived project
#run python code below
ldf=app.GetFromStudyCase('ComLdf') #caling loadflow command object
ldf.Execute() #executing the load flow command
#get the list of lines contained in the project
Lines=app.GetCalcRelevantObjects('*.ElmLne') #returns all relevant objects,
i.e. all lines
for line in Lines: #get each element out of list
name=line.loc_name #get name of the line
value=line.GetAttribute('c:loading') # return the value of elements
#Print the results
print('Loading of the line: %s = %.2f'%(name,value))
当上面的代码第一次在Spyder中执行时,它将显示正确的resutls。但是,如果再次重新执行脚本,则会出现以下错误:
Reloaded modules: powerfactory
Traceback (most recent call last):
File "<ipython-input-9-ae989570f05f>", line 1, in <module>
runfile('C:/Users/zd1n14/Desktop/Python Test/Call Digsilent in
Python.py', wdir='C:/Users/zd1n14/Desktop/Python Test')
File "C:\ProgramData\Anaconda3\lib\site-
packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "C:\ProgramData\Anaconda3\lib\site-
packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/zd1n14/Desktop/Python Test/Call Digsilent in Python.py",
line 12, in <module>
user=app.GetCurrentUser()
RuntimeError: 'powerfactory.Application' already deleted
提到How can I exit powerfactory using Python in Unattended mode?,这可能是因为PowerFactory在仍在运行。到目前为止,唯一找到的方法是重新启动Spyder并再次执行脚本,这是非常低的效率,如果我想重写代码并调试它。
如果有人能给我一些关于这个问题的建议的话,那就太过分了。
发布于 2017-05-22 10:42:17
我也遇到了同样的问题。Python仍然连接到powerfactory,如果您再次尝试连接,则会给出错误。对我来说最基本的方法是在你的剪裁结束时杀死这个实例
del app
调试期间的另一个想法可能是:
try:
# Do something in your skript
finally:
del app
所以在任何情况下都会杀死这个例子。
发布于 2017-05-22 11:29:32
https://stackoverflow.com/questions/44055764
复制相似问题