09-10-2020 02:17 AM
Hello,
I noticed that when I execute a python script with some import, the import persists even after the script ends. When reexecuting the script, the import is not done again.
This is not a standard python behavior and is not very convenient for code development. When I want to develop several connected python modules I do not really have a way how to do it. The only solution I found so far is to restart the whole DIAdem.
Is there an option to delete the imported modules?
Example to reproduce the issue:
File testImport.PY
from DIAdem import Application as dd
if False:
import DIAdem_CodeCompletion as dd
import testImport2
testImport2.print_msg()
File testImport2.PY in the same folder as the previous file
from DIAdem import Application as dd
if False:
import DIAdem_CodeCompletion as dd
def print_msg():
print('TEST')
When I execute the first file, I get the print 'TEST'. Now if I add print('TEST2') to the second file and reexecute the first file, I still only get one print - the second file was not reimported.
I tried using importlib.reload() function, but that does not work for a deeper structure of the imports - e.g. File A imports B which imports C. I modify the code in C. It is not enough to have the reload in File A in this case. Because I have much more files than 3, I do not even know where I should place the reload call to make the reload work.
Thanks for solution suggestions.
09-10-2020 05:22 AM
Hi,
DIAdem integrates Python into its own process to ensure fast access to functions and data.
With "normal" Python applications, Python.exe is terminated after each run of an application.
If several scripts are started in DIAdem one after the other, the Python engine cannot be reset in between. This is due to many existing modules (e.g. numpy) that load DLLs and initialize them correctly only once.
As a workaround for scripts changed in between I only know "importlib.reload()".
For the concrete example I would suggest not to use global code but to move the code into a function.
I hope the answer clarifies why the behavior is like this and the suggestion helps.
09-10-2020 08:53 AM
I do not know what you mean by "global code". I am trying to use a system of intertwined python libraries in DIAdem. There may be about 8 of those.
In my code I import one of them and that one import another etc.
So suppose my code SCRIPT.py imports library A, which imports B which imports C. When there is an error in C I would like to add some print messages in C to debug the issue. However I cannot just do that because of the permanent imports.
With the current functionality, I would have to use importlib.reload in SCRIPT.py for library A, in library A for library B and in library B for library C. That is not just inconvenient, but also hard to maintain and cleanup afterwards.
09-15-2020 03:58 AM
Hi SeryDavid, sorry for the late answer, I was not in the office.
By "global code" I mean the code that is executed during import.
But I guess the problem here is that a script is changed and Python doesn't notice the changes, the only thing that helps is "importlib.reload()".
Wouldn't it be an improvement if the reload is done in the main module during development, even if the module is not actually needed there?