# Gateway Script global name not defined

**URL:** <https://forum.inductiveautomation.com/t/gateway-script-global-name-not-defined/14819>\
**Category:** Ignition\
**Created:** [July 18, 2017, 6:12pm UTC](https://forum.inductiveautomation.com/t/gateway-script-global-name-not-defined/14819 "2017-07-18T18:12:50Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![curtis.barlow-wilkes](https://avatars.discourse-cdn.com/v4/letter/c/ed655f/32.png) [@curtis.barlow-wilkes](https://forum.inductiveautomation.com/u/curtis.barlow-wilkes)\
**Post date:** [July 18, 2017, 6:12pm UTC](https://forum.inductiveautomation.com/t/gateway-script-global-name-not-defined/14819/1 "2017-07-18T18:12:50Z")

</div>

I have recently moved a project script to a Tag Change Gateway Script so I could have it run without any clients open. But I’ve run into an issue that makes absolutely no sense “global name … not defined” and this was with globally declared functions I’ve tested the exact code within the script console and there were no issues. what do I make of this?  
This is the simplified code with the error with proprietary code stripped out:

```auto
def generateReport():
	return generateReportPart()
	
def generateReportPart():
	return ''

report = generateReport()

```

and the error:

`line 2, in generateReport NameError: global name 'generateReportPart' is not defined`

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [July 18, 2017, 6:27pm UTC](https://forum.inductiveautomation.com/t/gateway-script-global-name-not-defined/14819/2 "2017-07-18T18:27:19Z")

</div>

Yes. This is happening because there is a bug in Ignition with Gateway scripts. Gateways scripts still have the old legacy scoping enabled by default.

The bug is this: variables defined in the outermost scope can not be see by functions. In this case you have the generateReportPart variable that holds a function and you try to execute it in the generateReport function. This is perfect legal Python – but as I said the error occurs because of the scoping bug in Gateway scripts.

I wrote about the scope change in Ignition here: [http://www.perfectabstractions.com/blog/new-scripting-features-in-ignition-77-part-1#standard-scoping](http://www.perfectabstractions.com/blog/new-scripting-features-in-ignition-77-part-1#standard-scoping)

The best thing to do is write all your code in a project library function and then simply execute that function in your Gateway script.

---

<div class="post-metadata">

**Author:** ![1jeffwill11](https://avatars.discourse-cdn.com/v4/letter/1/b3f665/32.png) [@1jeffwill11](https://forum.inductiveautomation.com/u/1jeffwill11)\
**Post date:** [October 17, 2018, 3:55am UTC](https://forum.inductiveautomation.com/t/gateway-script-global-name-not-defined/14819/3 "2018-10-17T03:55:41Z")

</div>

Dead Thread I know, but I am having the exact same issue. What do you mean by a project library function?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [October 17, 2018, 12:10pm UTC](https://forum.inductiveautomation.com/t/gateway-script-global-name-not-defined/14819/4 "2018-10-17T12:10:16Z")

</div>

> [@1jeffwill11](#):
>
> What do you mean by a project library function?

Define functions in a project script module. Call those functions as `project.someName.functionName()`. In the designer, navigate to Project =\> Scripts =\> Script Library.

---

<div class="post-metadata">

**Author:** ![1jeffwill11](https://avatars.discourse-cdn.com/v4/letter/1/b3f665/32.png) [@1jeffwill11](https://forum.inductiveautomation.com/u/1jeffwill11)\
**Post date:** [October 17, 2018, 6:49pm UTC](https://forum.inductiveautomation.com/t/gateway-script-global-name-not-defined/14819/5 "2018-10-17T18:49:02Z")

</div>

Perfect!! Thank you.

---

<div class="post-metadata">

**Author:** ![ASF](https://avatars.discourse-cdn.com/v4/letter/a/c4cdca/32.png) [@ASF](https://forum.inductiveautomation.com/u/ASF)\
**Post date:** [October 25, 2018, 4:18am UTC](https://forum.inductiveautomation.com/t/gateway-script-global-name-not-defined/14819/6 "2018-10-25T04:18:18Z")

</div>

I’ve got the same issue happening. I have a script in my project script library called Test:

```auto
def testScript()
    print "script executed"
```

If I assign the following script to a button click, the script executes:

```auto
project.Test.testScript()
```

Now, I create a tag UDT, and on one member of that UDT, put the same code in a tag change event script:

`project.Test.testScript()`.

I create an instance of that UDT, and change the value of the tag with the tag change event script associated with it. The script does not execute, and the diagnostic log tells me “global name ‘project’ is not defined”.

Any ideas?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [October 25, 2018, 10:54am UTC](https://forum.inductiveautomation.com/t/gateway-script-global-name-not-defined/14819/7 "2018-10-25T10:54:55Z")

</div>

Tag Change Event Scripts are not the same thing as a Tag Event Value Change Script. That’s the point. Things attached to the tag definition or a UDT definition are outside the project system and therefore have no access to any project.\* scripts.  
Look at the gateway event scripts under the project’s “Scripting” section of the designer nav tree. Scripts defined there are part of the project, not the tag, and therefore have access to project script modules.

---

<div class="post-metadata">

**Author:** ![ASF](https://avatars.discourse-cdn.com/v4/letter/a/c4cdca/32.png) [@ASF](https://forum.inductiveautomation.com/u/ASF)\
**Post date:** [October 25, 2018, 9:38pm UTC](https://forum.inductiveautomation.com/t/gateway-script-global-name-not-defined/14819/8 "2018-10-25T21:38:13Z")

</div>

That makes sense, thanks for the explanation. My application really needs the script attached to the UDT definition, so perhaps I need to think about it differently. To avoid derailing this thread, I’ll start a new one and describe what I’m trying to achieve more fully.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [August 30, 2022, 2:26pm UTC](https://forum.inductiveautomation.com/t/gateway-script-global-name-not-defined/14819/9 "2022-08-30T14:26:36Z")

</div>

I’m having a mind-boggling issue…

I have this in a gateway tag change event:

```python
site_path = "foobar" # redacted
logger.info(site_path)
switched_off |= set(al.replace("Site", site_path) for al in switched_off)
switched_on |= set(al.replace("Site", site_path) for al in switched_on)

```

I get this error:  
`com.inductiveautomation.ignition.common.script.JythonExecException: NameError: global name 'site_path' is not defined `  
But I also get the log entry with `site_name`’s value…

Is it something with the generator expression ?  
What am I missing ?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [August 30, 2022, 2:30pm UTC](https://forum.inductiveautomation.com/t/gateway-script-global-name-not-defined/14819/10 "2022-08-30T14:30:08Z")

</div>

My intuition is that you’ve reduced or redacted this script before sharing with us to the point where you’ve removed the actual problem.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 30, 2022, 2:32pm UTC](https://forum.inductiveautomation.com/t/gateway-script-global-name-not-defined/14819/11 "2022-08-30T14:32:19Z")

</div>

Also, gateway events use legacy python scoping rules, which are weird. Move everything to a project library script function and retest.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [August 30, 2022, 2:34pm UTC](https://forum.inductiveautomation.com/t/gateway-script-global-name-not-defined/14819/12 "2022-08-30T14:34:25Z")

</div>

I hope legacy scoping isn’t _that_ weird 😬

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [August 30, 2022, 2:36pm UTC](https://forum.inductiveautomation.com/t/gateway-script-global-name-not-defined/14819/13 "2022-08-30T14:36:53Z")

</div>

I removed things before that snippet, that compute `switched_on` and `switched_off`, and a tag write after it, but nothing in that block has been modified/removed expect for the value of `site_path`, which was a literal too.

I’ll try moving things in the project library, but… still… wth ?  
the log line works properly, and the very next doesn’t !

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 30, 2022, 2:44pm UTC](https://forum.inductiveautomation.com/t/gateway-script-global-name-not-defined/14819/14 "2022-08-30T14:44:11Z")

</div>

Top level variables in legacy scoping are truly global, so can interact with other scripts using the same names (in parallel–multithreaded). ):

So, yes, that weird.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [August 30, 2022, 3:01pm UTC](https://forum.inductiveautomation.com/t/gateway-script-global-name-not-defined/14819/15 "2022-08-30T15:01:27Z")

</div>

Putting it in a project library seems to fix the issue, but, really…  
Why is the variable just fine on one line, then undefined on the next ?  
That’s not just weird. Weird was fine, but we left weird a few thousand miles back.

---

<div class="post-metadata">

**Author:** ![Joel\_Farley](https://avatars.discourse-cdn.com/v4/letter/j/f08c70/32.png) [@Joel\_Farley](https://forum.inductiveautomation.com/u/Joel_Farley)\
**Post date:** [June 29, 2023, 1:06am UTC](https://forum.inductiveautomation.com/t/gateway-script-global-name-not-defined/14819/16 "2023-06-29T01:06:51Z")

</div>

I've just had this same issue on a scheduled gateway script that was just calling a project script (v8.1.22). It only errored once and has been fine every other time.

Is there anything I can do to make this more robust? Or maybe a way to re-run a scheduled script if it fails?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [June 29, 2023, 2:28am UTC](https://forum.inductiveautomation.com/t/gateway-script-global-name-not-defined/14819/17 "2023-06-29T02:28:24Z")

</div>

> [@Joel\_Farley](#):
>
> Is there anything I can do to make this more robust?

Make all your event scripts one-liners that call a project library script function.

Always.

---

<div class="post-metadata">

**Author:** ![Glenn.G](https://avatars.discourse-cdn.com/v4/letter/g/bc79bd/32.png) [@Glenn.G](https://forum.inductiveautomation.com/u/Glenn.G)\
**Post date:** [July 4, 2023, 7:24am UTC](https://forum.inductiveautomation.com/t/gateway-script-global-name-not-defined/14819/18 "2023-07-04T07:24:57Z")

</div>

I'm so glad I've stumbled onto this thread, as I was about to lose my mind.  
I guess the same applies to python iterators running native in gateway event scripts?

```auto
result = system.db.runNamedQuery('Recipe/Select_UnknownMaterials', parameters)
	
unknownMaterials = list(result.getValueAt(i, y) for i in range(result.getRowCount()) for y in range(result.getColumnCount()))

```

Would end up with 'NameError: global name 'result' is not defined'.

What I don't understand however is why when moving the script into the project library it magically works?  
But thats most likely just me failing to understand the full extend of legacy scope vs standard scope.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [July 4, 2023, 7:38am UTC](https://forum.inductiveautomation.com/t/gateway-script-global-name-not-defined/14819/19 "2023-07-04T07:38:22Z")

</div>

may I suggest

```auto
from itertools import chain

return list(chain.from_iterable(system.dataset.toPyDataSet(result)))

```
