Solved

How to run the program by python more than twice?

  • 5 February 2024
  • 6 replies
  • 109 views

Dear all:

I'd like to run a program organized by python. 
But it can only be executed once as following.


In [1]: runfile('D:/untitled1.py', wdir='D:')

In [2]: runfile('D:/untitled1.py', wdir='D:')
Reloaded modules: CLR, clr, ZOSAPI_NetHelper, ZOSAPI
Traceback (most recent call last):

  File "D:\untitled1.py", line 139, in <module>
    zos = PythonStandaloneApplication()

  File "D:\untitled1.py", line 20, in __init__
    clr.AddReference(NetHelper)

AttributeError: module 'clr' has no attribute 'AddReference'


I use the anaconda 64 bits with spyder 5.3.3.
I have installed the pythonnet by 
pip install pythonnet

And it seems working normally so the program can be run once. 
But it cannot be run more than twice.

Does anyone encounter this problem before? 

icon

Best answer by David.Nguyen 5 February 2024, 16:37

View original

6 replies

Userlevel 7
Badge +2

@Cheng-Mu Tsai,

 

In Spyder, try: Consoles..Restart kernel between your calls. You will also need to re-activate the interactive extension in OpticStudio (manually). As long as you don’t restart the kernel you cannot have another connection to the ZOSAPI as it is limited to one.

Let me know if it helps.

Take care,

 

David

Hi, David,
Thanks for your reply. 
It can work by your solution. 
But I wonder why the consoles kernel needs to be restart every time. 
I have used the anaconda-32bits before and there is no any problems. 
Why does the program have a reloading ZOSAPI_NetHelper problem in 64 bits version?

Best wishes, 
CM tsai
 

Userlevel 7
Badge +2

@Cheng-Mu Tsai,

 

Unfortunately, I don’t have an answer to this. Is it a problem mostly because you are developing a new program and are running it several times in a row to see its output?

This is not related to the ZOSAPI directly, but this toppic was also discussed among ZOSPy developers (who are not ANSYS!) in this issue:

https://github.com/MREYE-LUMC/ZOSPy/issues/61

Take care,

 

David

Hi, David, 
Thanks for reply soon. 
I think that I have found the solution. 
The problem could be casused by the version of the pythonnet and python. 
I employ the python 3.7.16 and install the pythonnet 2.5.2 by
pip install pythonnet==2.5.2
Then, the program can run normally more than twice. 

Best Regards, 
CM tsai

Userlevel 3

Hi Cheng-Mu,

I just read your reply, and I’m not really sure if you are really running two independent versions of the program. For the development of ZOSPy we did some testing on this (as elaborated in the link above ), and although in some cases it might seem you are getting with a new/fresh connection, the underlying API actually returns the earlier created connection. As this could result in unreproducible behaviour (e.g. earlier simulations could effect the current session), I would advise to either restart the kernel between sessions (you are sure you have a fresh start) or just keep the first connection open, so it is clear from the code what is happening
 

Hi, jwbeenakker, 
Thanks for your response. 
When we develop a program, we always modify  and try runing the program again and again to comfirm the correction. 
For example, we could add a variable into the original program and then run the program. 
I don't know why the program can be run in the first time. 
After adding a varialbe into the same progrm, it cannot be executed  in the second running as shown as following. 

runfile('D:/GA24240.py', wdir='D:')
Exception ignored in: <function PythonStandaloneApplication.__del__ at 0x0000021D3D47EA60>
Traceback (most recent call last):
  File "D:\GA24240.py", line 65, in __del__
    if self.TheApplication is not None:
AttributeError: 'PythonStandaloneApplication' object has no attribute 'TheApplication'
Traceback (most recent call last):

  File "D:\GA24240.py", line 139, in <module>
    zos = PythonStandaloneApplication()

  File "D:\GA24240.py", line 20, in __init__
    clr.AddReference(NetHelper)

AttributeError: module 'clr' has no attribute 'AddReference'

I don't think that it is reasonable for restart the kernel. 
And why does the issue come from the pythonnet version 3.x.x rathan than 2.5.2?

Reply