2017-05-18 9 views
0

スクリプトは次のようにPythonが外部PoiwerFctoryと呼ばれるソフトウェアを実行することができます:インポートされたPowerFactoryモジュールがPythonで一度だけ実行できるのはなぜですか?

#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)) 

スパイダーで実行上記のコードでは初めて、それが適切なresutlsが表示されますとき。しかし、再度、スクリプトを再実行する場合は、次のエラーが登場している:

まだ実行中であるためPowerFactoryの How can I exit powerfactory using Python in Unattended mode?、この5月に言及
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 

。これまでに見つかった唯一の方法は、Spyderを再起動してスクリプトを再実行することです。これは非常に非効率的で、コードを書き直してデバッグする必要がある場合です。

誰でも私にそのような問題のアドバイスを与えることができれば、あまりにも充当されるだろう。

答えて

0

同じ問題が発生しました。 Pythonはまだpowerfactoryに接続されており、再度接続しようとするとエラーが発生します。私の仕事は何basiclyデバッグ中

del app 

別のアイデアをあなたのskriptの最後にインスタンスを殺すことだったかもしれない:

try: 
    # Do something in your skript 
finally: 
    del app 

だから、インスタンスの殺害は、どのような場合に起こります。

+0

ありがとうございました!それは動作し、私はあなたがそれを試してみたい場合、投稿された答えとして別の方法を見つけました。 – zdeng

0

これを解決する方法を追加することにより、powerfacotryモジュールをリロードすることです:

if __name__ == "__main__": 

輸入powerfacory前に。

理由は後で言及されるかもしれません:What does if __name__ == "__main__": do?

関連する問題