Removing "imports" from event handler scripts

We have an 'import sys' statement at the beginning of an event handler script (for propertyChange -- on a property that changes a lot). This is on a "high traffic" screen that drives a lot of our actual manufacturing processes. Since joining this forum I've learned that it is bad practice to do imports on an event handler.

My question is, could I simply add import sys (or whatever other module is needed) in a client startup script? After which point I could call the related functions in any script contained in the project?

Side note, I have the same dumb question but for tags too. If we have a tag change script firing every so often that contains an import statement, where can we even move the import statement to? A gateway script?

You would refactor the event handler scripts to call functions from a project library script (or as many as needed to stay well-organized) instead. At the top of that project library script, you would put your import statements, no client startup script required.

The same thing would apply here. Put the actual logic in the project library script and then call it from your tag change script. If it's a gateway tag change script, which it probably should be, change script on the tag itself (thanks @JordanCClark), you'd set up your project as the gateway scripting project in the gateway Config section.

As a bonus, this should make it easier to update any repeated code as it would only exist in one place.

1 Like

Yes, into the Gateway Scripting Project.


These can be scoped into the project. Scripts running from the tags themselves need to be scoped to the gateway scripting project.

1 Like

Thanks for that clarification! I got mixed up thinking that it was both tag and gateway change events that were gateway scoped.

They are both gateway scoped. But only the events on the tag themselves use the Gateway Scripting Project setting.


That makes sense, since the tags don't belong to any specific project.

Yeah, scoped was the wrong word to use. Reference would probably have been closer.

We had our grandson over the weekend, and I'm still recovering. :crazy_face: