# Execute Dynamic python code

**URL:** <https://forum.inductiveautomation.com/t/execute-dynamic-python-code/78789>\
**Category:** Ignition\
**Created:** [September 5, 2023, 8:56pm UTC](https://forum.inductiveautomation.com/t/execute-dynamic-python-code/78789 "2023-09-05T20:56:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dwoo](https://avatars.discourse-cdn.com/v4/letter/d/bbce88/32.png) [@dwoo](https://forum.inductiveautomation.com/u/dwoo)\
**Post date:** [September 5, 2023, 8:56pm UTC](https://forum.inductiveautomation.com/t/execute-dynamic-python-code/78789/1 "2023-09-05T20:56:06Z")

</div>

I would like to build a button template, which accepts as a custom parameter a string defining a  
project script to run. I tried using "exec()" but it doesn't seem to work (see test in Script console screen shot below), no print output...

i.e. In the project library define some functions. In the button template call the function passed to it in the custom parameter string...

Can't use runScript() since it's an expression function.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/9/f9a83f49bafc92592e4d57743c6644280450d7f0.png)

---

<div class="post-metadata">

**Author:** ![dwoo](https://avatars.discourse-cdn.com/v4/letter/d/bbce88/32.png) [@dwoo](https://forum.inductiveautomation.com/u/dwoo)\
**Post date:** [September 5, 2023, 9:00pm UTC](https://forum.inductiveautomation.com/t/execute-dynamic-python-code/78789/2 "2023-09-05T21:00:22Z")

</div>

Never mind...figured it out...had to put the "()" in the script call

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/3/c33cdae9d5104e666fcdab9b4d19b9e582d6a1f8.png)

---

<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:** [September 5, 2023, 9:03pm UTC](https://forum.inductiveautomation.com/t/execute-dynamic-python-code/78789/3 "2023-09-05T21:03:25Z")

</div>

> [@dwoo](#):
>
> Can't use runScript() since it's an expression function.

Why? This is literally what it does?

Also, you haven’t said where this “button” is going to reside, but note that the Script Console is not the same as the gateway.

---

<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:** [September 5, 2023, 9:18pm UTC](https://forum.inductiveautomation.com/t/execute-dynamic-python-code/78789/4 "2023-09-05T21:18:01Z")

</div>

> [@dwoo](#):
>
> which accepts as a custom parameter a string defining a  
> project script to run

This is an astonishingly bad idea. Consider allowing a function name that must **already exist** in the project, and then you don't need `exec()`. Instead, use python's `getattr` to look up the function name in the project library, which will give you a function object, which you can then run directly.

---

<div class="post-metadata">

**Author:** ![dwoo](https://avatars.discourse-cdn.com/v4/letter/d/bbce88/32.png) [@dwoo](https://forum.inductiveautomation.com/u/dwoo)\
**Post date:** [September 5, 2023, 9:50pm UTC](https://forum.inductiveautomation.com/t/execute-dynamic-python-code/78789/5 "2023-09-05T21:50:30Z")

</div>

The button will be in a template screen. The button will have other scripting so can't just call an expression and use runScript().

I will try using the getattr instead of the exec() call...  
Thanks.

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [September 6, 2023, 5:16am UTC](https://forum.inductiveautomation.com/t/execute-dynamic-python-code/78789/6 "2023-09-06T05:16:05Z")

</div>

The use of exec and eval are considered to be a bad practice due to injection risks. Even if it seems safe for use in your smoker oven project, I still recommend avoiding unsafe practices for the sake of developing good habits.

---

<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:** [September 6, 2023, 1:58pm UTC](https://forum.inductiveautomation.com/t/execute-dynamic-python-code/78789/7 "2023-09-06T13:58:31Z")

</div>

Agree.

I would put this script in a library, and make a single call from the button. I see absolutely no reason for this to be "dynamic".
