Hello,
I was developing in Ignition 8.1.38 an object-oriented function in Jython and noticed that importing project modules resolves some seemingly random problems that only occur when testing from the Script Console.
I'd like to ask if you see any issues with leaving these imports enabled in the modules, as it appears to work the same way in the Designer's scripting console and in the Gateway.
Apart from the fact that when Ignition detects changes in the modules, it recompiles and restarts the script instance and everything starts from scratch.
Here's a simple example of what I've observed:
I have a package called modules with the modules modBase, mod1, and mod2.
mod1 and mod2 are identical and access modBase functionalities.
# modBase code
myInternalVar = 0
class myClass():
pass
def incAndGet():
modules.modBase.myInternalVar += 1
return modules.modBase.myInternalVar
logger = system.util.getLogger("TEST")
logger.info("modules.modBase loaded")
# mod1 and mod2 code
#import modules
def incAndGet():
return modules.modBase.incAndGet()
def getClass():
return modules.modBase.myClass
# Test code, tested in Script Console and Gateway Event Handler
logger = system.util.getLogger("TEST")
logger.info("mod1.increment and get: " + str(modules.mod1.incAndGet()))
logger.info("mod2.increment and get: " + str(modules.mod2.incAndGet()))
logger.info("myClass id from mod1: " + str(id(modules.mod1.getClass())))
logger.info("myClass id from mod2: " + str(id(modules.mod2.getClass())))
logger.info("myClass id from modBase: " + str(id(modules.modBase.myClass)))
inst1 = modules.mod1.getClass()() # create instance
logger.info(
"instance of myClass isinstance of myClass: "
+ str(isinstance(inst1, modules.modBase.myClass))
)
Results After running first time after saving in Designer ("script recompiled and restart")
Without importing
Importing modules
The result does not change at the gateway.
Therefore, to avoid discrepancies between tests performed in Designer and those executed in Gateway, I think it's best to maintain the imports.
An interesting point is that initially, I was importing the exact module I needed:
import modules.modBase
This created a separate entry for each imported module name in sys.modules.
But then I accidentally discovered that importing the package is sufficient to prevent this from happening. This also prevents the fully qualified module names from being loaded into sys.modules.
So let me know if you see any problems with this.
Thank you in advance


