# Calling a class from a script

**URL:** <https://forum.inductiveautomation.com/t/calling-a-class-from-a-script/71225>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [March 17, 2023, 11:48am UTC](https://forum.inductiveautomation.com/t/calling-a-class-from-a-script/71225 "2023-03-17T11:48:44Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert\_Collinson](https://avatars.discourse-cdn.com/v4/letter/r/7993a0/32.png) [@Robert\_Collinson](https://forum.inductiveautomation.com/u/Robert_Collinson)\
**Post date:** [March 17, 2023, 11:48am UTC](https://forum.inductiveautomation.com/t/calling-a-class-from-a-script/71225/1 "2023-03-17T11:48:44Z")

</div>

If I have created a class and saved it into it's own script within Ignition Designer, can it be 'imported' from another script in the same way as you could when writing 'py' scripts (i.e. placing all py's in the same directory and simply using the import command against the 'py' file containing the class to gain access to the class)?

If so, what would the construct of the filename need to be in this instance?

Say, for example, I nested it within a different sub-directory of the script library away from the sub-directory within which my script was in, how could I point the import command to it?

I've been looking through the knowledgebase and at Inductive University and I'm struggling to find any reference to the use of home-brew classes within Ignition.

It seems to work just fine when placed within the script it's used....however, as my class has been designed for use in multiple different circumstances, I suppose it really should be imported when required only to keep the script I'm using it within clean.

Cheers for any feedback

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [March 17, 2023, 12:34pm UTC](https://forum.inductiveautomation.com/t/calling-a-class-from-a-script/71225/2 "2023-03-17T12:34:46Z")

</div>

While you are in the script project library where your class is defined, just right click on the class on the right-hand side of the editor and select copy path. That will show you the full path to the class which is how you can use it in other script packages etc.

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/6/86682b9722e50419ce9486331cc977401d664ffb.png)

---

<div class="post-metadata">

**Author:** ![Robert\_Collinson](https://avatars.discourse-cdn.com/v4/letter/r/7993a0/32.png) [@Robert\_Collinson](https://forum.inductiveautomation.com/u/Robert_Collinson)\
**Post date:** [March 17, 2023, 12:52pm UTC](https://forum.inductiveautomation.com/t/calling-a-class-from-a-script/71225/3 "2023-03-17T12:52:05Z")

</div>

...and i just copy that and list it as an include at the top?

If so, that's cool...nice and simple.

I'd forgotten that was an option to be fair...my question was more along the lines though, as to whether an include would be tolerant of such a path.

I _think_ it should be as I've just spotted some code used by one of our Dev's which suggest it would be.

Thanks again!

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [March 17, 2023, 1:07pm UTC](https://forum.inductiveautomation.com/t/calling-a-class-from-a-script/71225/4 "2023-03-17T13:07:07Z")

</div>

You shouldn't need to _include_ it, just use the full path in the script where you need it.

---

<div class="post-metadata">

**Author:** ![Robert\_Collinson](https://avatars.discourse-cdn.com/v4/letter/r/7993a0/32.png) [@Robert\_Collinson](https://forum.inductiveautomation.com/u/Robert_Collinson)\
**Post date:** [March 17, 2023, 1:21pm UTC](https://forum.inductiveautomation.com/t/calling-a-class-from-a-script/71225/5 "2023-03-17T13:21:44Z")

</div>

Ahh, you see, this is right in to the crux of my question.

I don't want to keep using the path to the script...I just want to use the methods within it.

This is where an include statement comes into it's own...declare it once, and, then refer to the methods many times throughout.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [March 17, 2023, 1:25pm UTC](https://forum.inductiveautomation.com/t/calling-a-class-from-a-script/71225/6 "2023-03-17T13:25:31Z")

</div>

Edit: don't do this as pointed out below  
~~  
import shared.scriptPackage.someClass as someName  
someName.method()  
 ~~

---

<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:** [March 17, 2023, 1:57pm UTC](https://forum.inductiveautomation.com/t/calling-a-class-from-a-script/71225/7 "2023-03-17T13:57:59Z")

</div>

> [@Robert\_Collinson](#):
>
> I don't want to keep using the path to the script...I just want to use the methods within it.

Sorry, you should not use import with project library scripts. There are odd corner cases that can eat your lunch. Use the full path everywhere you need it.

---

<div class="post-metadata">

**Author:** ![Robert\_Collinson](https://avatars.discourse-cdn.com/v4/letter/r/7993a0/32.png) [@Robert\_Collinson](https://forum.inductiveautomation.com/u/Robert_Collinson)\
**Post date:** [March 17, 2023, 1:59pm UTC](https://forum.inductiveautomation.com/t/calling-a-class-from-a-script/71225/8 "2023-03-17T13:59:00Z")

</div>

oh...ok.

Which 'corner cases' may these be then?

---

<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:** [March 17, 2023, 2:04pm UTC](https://forum.inductiveautomation.com/t/calling-a-class-from-a-script/71225/9 "2023-03-17T14:04:48Z")

</div>

Misordered/unordered script loading that leaves old old code alive in strange ways. Never really nailed down, **but suspected to be an artifact of java multithreading with jython's import machinery**. Virtually impossible to reliably repeat, which is why I don't trust it.

It doesn't happen if you always refer to other project scripts' objects with full paths, at least for first use in a function.

Edit: New bold above. Now nailed down to be a bug in Ignition's script library auto-import, with a work-around:

> [@Project Library Script Initialization Races](https://forum.inductiveautomation.com/t/project-library-script-initialization-races/98636):
>
> Are you annoyed by pseudo-random breakage in Ignition project library scripting? Odd top-level constants being garbled, or operations in the top level that have side effects running twice? Or more? Not happening much, but always around project edits, and sometimes even on gateway restart? Yeah, me too. frowning_face Poking around under the hood back in the spring, for a module development task, I ran across some problematic IA bytecode and pinged some people. But it went nowhere, as I co…

---

<div class="post-metadata">

**Author:** ![Robert\_Collinson](https://avatars.discourse-cdn.com/v4/letter/r/7993a0/32.png) [@Robert\_Collinson](https://forum.inductiveautomation.com/u/Robert_Collinson)\
**Post date:** [March 22, 2023, 8:00am UTC](https://forum.inductiveautomation.com/t/calling-a-class-from-a-script/71225/10 "2023-03-22T08:00:27Z")

</div>

do Python 'deconstructor' methods not work to close out these open threads then?

for e.g.:

> del named class object

---

<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:** [March 22, 2023, 9:29am UTC](https://forum.inductiveautomation.com/t/calling-a-class-from-a-script/71225/11 "2023-03-22T09:29:44Z")

</div>

How about this ?

```python
SimplerClass = SomePackage.SomeFile.SomeClass

foo = SimplerClass()
bar = SimplerClass()

```

Could that cause issues as well ?

---

<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:** [March 22, 2023, 11:36am UTC](https://forum.inductiveautomation.com/t/calling-a-class-from-a-script/71225/12 "2023-03-22T11:36:19Z")

</div>

> [@Robert\_Collinson](#):
>
> not work to close out these open threads then?

No. You must `.interrupt()` them, and they must catch that, and exit voluntarily.

---

<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:** [March 22, 2023, 11:37am UTC](https://forum.inductiveautomation.com/t/calling-a-class-from-a-script/71225/13 "2023-03-22T11:37:26Z")

</div>

> [@pascal.fragnoud](#):
>
> How about this ?

Possibly OK. Certainly OK if that first assignment is within a function call (to serve within the function) instead of at the top level.
