# Dynamic import script module, importlib can be used?

**URL:** <https://forum.inductiveautomation.com/t/dynamic-import-script-module-importlib-can-be-used/97330>\
**Category:** Ignition\
**Tags:** ignition81, scripting\
**Created:** [October 16, 2024, 4:40pm UTC](https://forum.inductiveautomation.com/t/dynamic-import-script-module-importlib-can-be-used/97330 "2024-10-16T16:40:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [October 16, 2024, 4:40pm UTC](https://forum.inductiveautomation.com/t/dynamic-import-script-module-importlib-can-be-used/97330/1 "2024-10-16T16:40:16Z")

</div>

I try to execute a function name that can be configured, for example in a dictionnary.

It seems to be ok in the designer:

```python
import importlib
name = "custom.util"
mod = importlib.import_module(name)
print mod.formatData(3254)

```

@pturmel, @paul-griffith  
is there any problem to use **importlib ?**

Ignition Project **Final** inherite from Project **Global**  
Script function from project **Global** can call function declared in Project **Final**

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [October 16, 2024, 5:03pm UTC](https://forum.inductiveautomation.com/t/dynamic-import-script-module-importlib-can-be-used/97330/2 "2024-10-16T17:03:52Z")

</div>

The only issue I can see is if the package you're trying to import isn't available in the JVM where the interpreter is running. For instance a package available in Gateway Scope may not be available in Client Scope, or a Package may not be available to all clients. If the code will always be running in the same JVM and you know the package will be available, then it should work just fine.

I would still wrap it in a try/except block with logging to indicate if something goes wrong trying to import the package.

---

<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 16, 2024, 6:09pm UTC](https://forum.inductiveautomation.com/t/dynamic-import-script-module-importlib-can-be-used/97330/3 "2024-10-16T18:09:54Z")

</div>

> [@mazeyrat](#):
>
> is there any problem to use **importlib ?**

Not sure. I have my doubts.

You certainly should **never** import any Ignition project library script. **Never**. (Ignition uses its own "import" machinery.)

> [@mazeyrat](#):
>
> Script function from project **Global** can call function declared in Project **Final**

Sure, as long as the ultimate origin of the call stack is in project Final.

Such a function in Global would depend on the ultimate caller's context to have an implementation of the named function. Different leaf projects could have different implementations.

Edit: This long-standing script library bug in Ignition's "auto import" behavior is relevant here:

> [@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:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [October 16, 2024, 8:00pm UTC](https://forum.inductiveautomation.com/t/dynamic-import-script-module-importlib-can-be-used/97330/4 "2024-10-16T20:00:06Z")

</div>

Thanks for the reply  
"Ignition uses its own "import" machinery."  
That's exactly why I'm not very confident to use **importlib**...

The caller context, the final project has an implementation of the named function.

The use case was to create a generic perspective page message handler with in the message payload the module name, fonction name and args (handler in the global project). Action of the page message handler would be to dynamically import the module name provided and call the function with args.

(probably influenced by the function pointers in the C language...)

---

<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 16, 2024, 8:10pm UTC](https://forum.inductiveautomation.com/t/dynamic-import-script-module-importlib-can-be-used/97330/5 "2024-10-16T20:10:21Z")

</div>

Don't import. Don't do function lookup. Simply have the caller **supply the function**. Not the function _name_, but the function _object_. Named functions, without the parentheses, _ **are** _ function pointers in python. They are first-class objects.

Consider re-architecting your global functionality as _ **decorators** _, and simply _annotate_ the leaf project's library functions with the desired common behavior. Then the message handler simply delegates (one-liners of course) to a project library script function as usual.

---

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [October 16, 2024, 8:15pm UTC](https://forum.inductiveautomation.com/t/dynamic-import-script-module-importlib-can-be-used/97330/6 "2024-10-16T20:15:06Z")

</div>

I'm going that way 👍
