# "Global name 'world' is not defined" in Client Tag Change script

**URL:** <https://forum.inductiveautomation.com/t/global-name-world-is-not-defined-in-client-tag-change-script/25982>\
**Category:** Ignition\
**Created:** [June 20, 2019, 1:48pm UTC](https://forum.inductiveautomation.com/t/global-name-world-is-not-defined-in-client-tag-change-script/25982 "2019-06-20T13:48:35Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ecan](https://avatars.discourse-cdn.com/v4/letter/e/8edcca/32.png) [@ecan](https://forum.inductiveautomation.com/u/ecan)\
**Post date:** [June 20, 2019, 1:48pm UTC](https://forum.inductiveautomation.com/t/global-name-world-is-not-defined-in-client-tag-change-script/25982/1 "2019-06-20T13:48:35Z")

</div>

Hi,

I have a piece of code that works fine in `actionPerformed` of components. The same exact code leads to “Global name ‘world’ is not defined” in my `Client Tag Change` script. Any idea why?

**Code**

```auto
from project.hello import world
try:
    def long_process():
        success = world()

        def send_back(success=success):
            if success:
                ...
            else:
                ...
        system.util.invokeLater(send_back)
    system.util.invokeAsynchronous(long_process)
except:
    ...

```

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [June 20, 2019, 1:56pm UTC](https://forum.inductiveautomation.com/t/global-name-world-is-not-defined-in-client-tag-change-script/25982/2 "2019-06-20T13:56:06Z")

</div>

There is a scoping issue:

> [@Gateway Script global name not defined](http://forum.inductiveautomation.com/t/gateway-script-global-name-not-defined/14819/2):
>
> 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…

---

<div class="post-metadata">

**Author:** ![ecan](https://avatars.discourse-cdn.com/v4/letter/e/8edcca/32.png) [@ecan](https://forum.inductiveautomation.com/u/ecan)\
**Post date:** [June 20, 2019, 9:10pm UTC](https://forum.inductiveautomation.com/t/global-name-world-is-not-defined-in-client-tag-change-script/25982/3 "2019-06-20T21:10:25Z")

</div>

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![ecan](https://avatars.discourse-cdn.com/v4/letter/e/8edcca/32.png) [@ecan](https://forum.inductiveautomation.com/u/ecan)\
**Post date:** [June 21, 2019, 9:24am UTC](https://forum.inductiveautomation.com/t/global-name-world-is-not-defined-in-client-tag-change-script/25982/4 "2019-06-21T09:24:49Z")

</div>

> [@ecan](#):
>
> success=success)

Can I call `success` in `send_back` when I am not passing it as a parameter?

---

<div class="post-metadata">

**Author:** ![ecan](https://avatars.discourse-cdn.com/v4/letter/e/8edcca/32.png) [@ecan](https://forum.inductiveautomation.com/u/ecan)\
**Post date:** [June 21, 2019, 9:33am UTC](https://forum.inductiveautomation.com/t/global-name-world-is-not-defined-in-client-tag-change-script/25982/5 "2019-06-21T09:33:45Z")

</div>

I’ve read this topic, but It’s not clear to me how I can solve this? Could you please elaborate?

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [June 21, 2019, 12:20pm UTC](https://forum.inductiveautomation.com/t/global-name-world-is-not-defined-in-client-tag-change-script/25982/6 "2019-06-21T12:20:24Z")

</div>

The import doesn’t work because it doesn’t scope into your functions. You need to specifically reference it each time you use it:

```python
try:
    def long_process():
        success = project.hello.world()

        def send_back(success=success):
            if success:
                ...
            else:
                ...
        system.util.invokeLater(send_back)
    system.util.invokeAsynchronous(long_process)
except:
    ...

```

You should still be able to use success as a parameter. Also, since you are using this in an event script, I don’t think you’ll need to use invokeLater, since it will run in a different thread.

---

<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 21, 2019, 1:12pm UTC](https://forum.inductiveautomation.com/t/global-name-world-is-not-defined-in-client-tag-change-script/25982/7 "2019-06-21T13:12:19Z")

</div>

> [@JordanCClark](#):
>
> You need to specifically reference it each time you use it:

This isn't strictly true. Inner functions have access to outer scope variables captured at the time of definition in their _closures_. In modern python. Legacy scoping is funky. That's one of the reasons later.py is a script module -- modern scoping applies no matter what calls into those functions.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [June 21, 2019, 1:20pm UTC](https://forum.inductiveautomation.com/t/global-name-world-is-not-defined-in-client-tag-change-script/25982/8 "2019-06-21T13:20:40Z")

</div>

Thanks for the clarification, Phil. What about the use of invokeLater in a Client Tag Change event script?

---

<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 21, 2019, 4:02pm UTC](https://forum.inductiveautomation.com/t/global-name-world-is-not-defined-in-client-tag-change-script/25982/9 "2019-06-21T16:02:42Z")

</div>

I don’t remember which of those events use legacy scoping. If it isn’t a component event, I always place the actual event code in a project script module. Modern scoping is then applicable and closures work as expected.

---

<div class="post-metadata">

**Author:** ![ecan](https://avatars.discourse-cdn.com/v4/letter/e/8edcca/32.png) [@ecan](https://forum.inductiveautomation.com/u/ecan)\
**Post date:** [July 2, 2019, 9:07am UTC](https://forum.inductiveautomation.com/t/global-name-world-is-not-defined-in-client-tag-change-script/25982/10 "2019-07-02T09:07:54Z")

</div>

> [@JordanCClark](#):
>
> project.hello.world()

This doesn't work. Leads to `global name 'project' is not defined`

---

<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:** [July 2, 2019, 12:39pm UTC](https://forum.inductiveautomation.com/t/global-name-world-is-not-defined-in-client-tag-change-script/25982/11 "2019-07-02T12:39:14Z")

</div>

> [@ecan](#):
>
> This doesn’t work.

You aren't showing your code, but I'm going to guess that your project.hello.world() call is within another function you defined within the event. Legacy scoping basically prevents any function defined within the event from having `system`, `shared`, or `project` automatically present. They'll only be present at the top level.

Make your event a one-liner, passing the event to a project script:

```auto
project.someModule.someEventHandlerFunction(event)

```

If the event has other local variables, pass them to your function, too, if you need them. Do everything in that function.

Otherwise, import system, shared, and project in every nested function that needs them.
