Project and shared scripts - scripting outside of functions

I’ve only used project & shared scripts by referring directly to a function inside them. I know you can declare variables outside of the functions, but when does that declaration run?

To clarify, lets say I have a function in a script that I’d access by shared.TestLibrary.doFunc(). If I had lines in the script called “TestLibrary”, when would they actually run? I think I’ve seen it used before but can’t find it.

I’m not sure how ignition handles this, but in general if I have 2 files:

import_file.py
print "hello"

*main_file.py"
import import_file.py

When main_file.py is run, the result will be “hello”. This is due to python being an interpreted vs compiled lanugage. I’m not sure how this is impacted with Jython and Ignition. You could run this same test in ignition though.

The entire script module is executed upon first reference, including assignments and the def statements that place place the functions into the script module’s namespace alongside the assignments. The namespace persists in the client until the client is restarted. It persists in the designer until the project is refreshed/opened. It persists in the gateway until some event requires an update to scripting, like when a script is saved from a designer or a module is loaded/unloaded.