How to import a Project Library script inside a tag readAsync callback

I'm using Ignition 8.1 and I want to run a script from Project Library in a Gateway Event, inside a system.tag.readAsync callback. I've noticed that I can't directly use libraries inside the callback and need to import them first, like with the System library.

In the example below, I get a NameError: Api. How do I import Api, which is a script/folder inside the Project Library? (import Api doesn't work)

def processTagReads(qvAsyncResults):
  import system
  saved = Api.saveOrderConsumption(qvAsyncResults[2].value, qvAsyncResults[3].value)
  system.tag.writeBlocking("[default]AGROMATIC/bal 0/saved", saved)

system.tag.readAsync(tagPaths, processTagReads)

(Currently) gateway events follow a weird, legacy Python scoping behavior that causes behaviors like this. (Currently) the only way to avoid this is to put at least any internal functions you author into the project library themselves, and/or the entire body of your event script such that the only thing actually in the gateway event script is a single line call in to your project library.

I stress currently because we're (finally) removing this limitation in 8.3.

4 Likes