为什么python中导入的PowerFactory模块只能执行一次?
Why the imported PowerFactory module in python can only execute single time?
该脚本可以运行外部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中执行时,会显示正确的结果。但是,如果再次重新执行脚本,会出现如下错误:
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还在运行ning。而目前找到的唯一方法就是重启Spyder再执行脚本,这样效率太低了,如果要重写代码调试的话
如果有人能给我一些关于此类问题的建议,那将是非常合适的。
我运行遇到同样的问题。 Python 仍连接到 powerfactory,如果您尝试再次连接,则会出现错误。对我来说基本上有用的是用
杀死你的脚本末尾的实例
del app
调试期间的另一个想法可能是:
try:
# Do something in your skript
finally:
del app
所以实例被杀是无论如何都会发生的。
解决这个问题的方法是通过添加重新加载 powerfacotry 模块:
if __name__ == "__main__":
在导入 powerfacory 之前。
原因可参考:What does if __name__ == "__main__": do?.
该脚本可以运行外部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中执行时,会显示正确的结果。但是,如果再次重新执行脚本,会出现如下错误:
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还在运行ning。而目前找到的唯一方法就是重启Spyder再执行脚本,这样效率太低了,如果要重写代码调试的话
如果有人能给我一些关于此类问题的建议,那将是非常合适的。
我运行遇到同样的问题。 Python 仍连接到 powerfactory,如果您尝试再次连接,则会出现错误。对我来说基本上有用的是用
杀死你的脚本末尾的实例del app
调试期间的另一个想法可能是:
try:
# Do something in your skript
finally:
del app
所以实例被杀是无论如何都会发生的。
解决这个问题的方法是通过添加重新加载 powerfacotry 模块:
if __name__ == "__main__":
在导入 powerfacory 之前。
原因可参考:What does if __name__ == "__main__": do?.