该脚本能够通过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并再次执行脚本,如果我想重新编写代码并对其进行调试,这是非常低效的。
如果有人能就这样的问题给我一些建议,那将是非常适当的。
答案 0 :(得分:0)
我遇到了同样的问题。 Python仍然连接到powerfactory,如果你再次尝试连接,则会给出错误。对我来说基本起作用的是用你的
来杀死你的实例del app
调试期间的另一个想法可能是:
try:
# Do something in your skript
finally:
del app
因此,无论如何都会杀死实例。
答案 1 :(得分:0)
解决此问题的方法是通过添加:
重新加载powerfacotry模块if __name__ == "__main__":
在导入powerfacory之前。
背后的原因可能是:What does if __name__ == "__main__": do?。