# Executing Python from Module

**URL:** <https://forum.inductiveautomation.com/t/executing-python-from-module/6073>\
**Category:** 3rd Party Modules\
**Created:** [March 21, 2013, 2:44pm UTC](https://forum.inductiveautomation.com/t/executing-python-from-module/6073 "2013-03-21T14:44:26Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![chuckcoughlin](https://avatars.discourse-cdn.com/v4/letter/c/e19b73/32.png) [@chuckcoughlin](https://forum.inductiveautomation.com/u/chuckcoughlin)\
**Post date:** [March 21, 2013, 2:44pm UTC](https://forum.inductiveautomation.com/t/executing-python-from-module/6073/1 "2013-03-21T14:44:26Z")

</div>

I’d like to execute a Gateway script from inside my module. Is the ScriptManager method runFunction() designed to do this? If not can I create code that imports an existing Python script and use runCode? Am not at all clear from Javadoc on how to accomplish this.

Thanks in advance for your help.  
— chuck

---

<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:** [March 21, 2013, 2:56pm UTC](https://forum.inductiveautomation.com/t/executing-python-from-module/6073/2 "2013-03-21T14:56:12Z")

</div>

The runCode() function would let you attempt to run some arbitrary Jython code; is that what you’re asking? Or are you trying to run some project’s existing gateway script?

---

<div class="post-metadata">

**Author:** ![chuckcoughlin](https://avatars.discourse-cdn.com/v4/letter/c/e19b73/32.png) [@chuckcoughlin](https://forum.inductiveautomation.com/u/chuckcoughlin)\
**Post date:** [March 21, 2013, 3:27pm UTC](https://forum.inductiveautomation.com/t/executing-python-from-module/6073/3 "2013-03-21T15:27:37Z")

</div>

I’d like to execute a Gateway script that is pre-existing — written outside the module.  
— chuck

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [March 21, 2013, 3:32pm UTC](https://forum.inductiveautomation.com/t/executing-python-from-module/6073/4 "2013-03-21T15:32:50Z")

</div>

what sort of gateway script precisely are you talking about that you want to invoke?

---

<div class="post-metadata">

**Author:** ![chuckcoughlin](https://avatars.discourse-cdn.com/v4/letter/c/e19b73/32.png) [@chuckcoughlin](https://forum.inductiveautomation.com/u/chuckcoughlin)\
**Post date:** [March 21, 2013, 3:54pm UTC](https://forum.inductiveautomation.com/t/executing-python-from-module/6073/5 "2013-03-21T15:54:32Z")

</div>

These are user-written scripts sitting in my “Script Modules” node in the Designer nav tree. I believe these are what the documentation refers to as “Gateway Scripts”. I need to pass the data into the Python, but have no need for a response.

– chuck

---

<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:** [March 21, 2013, 4:02pm UTC](https://forum.inductiveautomation.com/t/executing-python-from-module/6073/6 "2013-03-21T16:02:31Z")

</div>

Scripts written in the script modules area are reusable bits of code that can be used from both client and gateway scopes. If something in the manual is referring to these as “gateway scripts” then it’s incorrect.

You should be able to run one of these modules like this, assuming you’ve got yourself a ScriptManager instance.

```auto
scriptManager.runCode("app.WhateverModule.whateverUserCreatedFunctionYouWant()", scriptManager.createLocalsMap(), "some-arbitrary-filename");
```

---

<div class="post-metadata">

**Author:** ![chuckcoughlin](https://avatars.discourse-cdn.com/v4/letter/c/e19b73/32.png) [@chuckcoughlin](https://forum.inductiveautomation.com/u/chuckcoughlin)\
**Post date:** [March 21, 2013, 4:16pm UTC](https://forum.inductiveautomation.com/t/executing-python-from-module/6073/7 "2013-03-21T16:16:01Z")

</div>

thanx, I’ll give it a shot.  
— chuck

---

<div class="post-metadata">

**Author:** ![chuckcoughlin](https://avatars.discourse-cdn.com/v4/letter/c/e19b73/32.png) [@chuckcoughlin](https://forum.inductiveautomation.com/u/chuckcoughlin)\
**Post date:** [March 22, 2013, 9:05pm UTC](https://forum.inductiveautomation.com/t/executing-python-from-module/6073/8 "2013-03-22T21:05:49Z")

</div>

`scriptmgr.runCode("app.ils.callback.handleCompletion",mgr.createLocalsMap(),"xxx");` gives a JythonExecException: File “xxx” in line 1 in , name “app” is not defined"

It seems as if this call expects Python code in an external file, whereas my code is, in the designer, in Configuration-\>Script Modules in a package app.lis.callback. The callback package has a method defined “handleCompletion”.

— chuck

---

<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:** [March 22, 2013, 9:39pm UTC](https://forum.inductiveautomation.com/t/executing-python-from-module/6073/9 "2013-03-22T21:39:19Z")

</div>

[quote=“chuckcoughlin”]`scriptmgr.runCode("app.ils.callback.handleCompletion",mgr.createLocalsMap(),"xxx");` gives a JythonExecException: File “xxx” in line 1 in , name “app” is not defined"

It seems as if this call expects Python code in an external file, whereas my code is, in the designer, in Configuration-\>Script Modules in a package app.lis.callback. The callback package has a method defined “handleCompletion”.

— chuck[/quote]

Are ‘mgr’ and ‘scriptmgr’ the same instances of ScriptManager? What’s going on there? And why doesn’t handleCompletion have the open/close paranethesis?

---

<div class="post-metadata">

**Author:** ![chuckcoughlin](https://avatars.discourse-cdn.com/v4/letter/c/e19b73/32.png) [@chuckcoughlin](https://forum.inductiveautomation.com/u/chuckcoughlin)\
**Post date:** [March 22, 2013, 9:54pm UTC](https://forum.inductiveautomation.com/t/executing-python-from-module/6073/10 "2013-03-22T21:54:06Z")

</div>

The mgr/scriptmgr are the ScriptManagers gotten from the GatewayContext. My bad. Added (), same result.

---

<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:** [March 22, 2013, 10:33pm UTC](https://forum.inductiveautomation.com/t/executing-python-from-module/6073/11 "2013-03-22T22:33:30Z")

</div>

I’m just concerned that you have two of them. That shouldn’t be the case…

---

<div class="post-metadata">

**Author:** ![chuckcoughlin](https://avatars.discourse-cdn.com/v4/letter/c/e19b73/32.png) [@chuckcoughlin](https://forum.inductiveautomation.com/u/chuckcoughlin)\
**Post date:** [March 22, 2013, 11:45pm UTC](https://forum.inductiveautomation.com/t/executing-python-from-module/6073/12 "2013-03-22T23:45:53Z")

</div>

Not at all, I was just trying to spruce up the code for public display, and messed up. Here it is unaltered:

`mgr.runCode(scriptName,mgr.createLocalsMap(),null);` where script name is “app.ils.callback.handleCompletion()” and I’ve tried various values for the file name (a file name argument doesn’t make sense to me(??)).

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [March 29, 2013, 4:26pm UTC](https://forum.inductiveautomation.com/t/executing-python-from-module/6073/13 "2013-03-29T16:26:54Z")

</div>

Don’t get thrown off by the filename thing. Python just wants something to put in tracebacks if there’s an error. It’s not used in any meaningful way.

Where are you getting a hold of this script manager?

---

<div class="post-metadata">

**Author:** ![chuckcoughlin](https://avatars.discourse-cdn.com/v4/letter/c/e19b73/32.png) [@chuckcoughlin](https://forum.inductiveautomation.com/u/chuckcoughlin)\
**Post date:** [March 29, 2013, 4:46pm UTC](https://forum.inductiveautomation.com/t/executing-python-from-module/6073/14 "2013-03-29T16:46:52Z")

</div>

```auto
ScriptManager mgr = context.getScriptManager(); 
```

where context is the gateway context.

– chuck

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [March 29, 2013, 6:04pm UTC](https://forum.inductiveautomation.com/t/executing-python-from-module/6073/15 "2013-03-29T18:04:05Z")

</div>

There’s your problem. You’re grabbing the gateway’s general script executor, but you’re trying to execute a global script that is part of a project. You need to grab that project’s script manager:

context.getProjectManager().getProjectScriptManager(projectId)

---

<div class="post-metadata">

**Author:** ![chuckcoughlin](https://avatars.discourse-cdn.com/v4/letter/c/e19b73/32.png) [@chuckcoughlin](https://forum.inductiveautomation.com/u/chuckcoughlin)\
**Post date:** [March 30, 2013, 2:38am UTC](https://forum.inductiveautomation.com/t/executing-python-from-module/6073/16 "2013-03-30T02:38:23Z")

</div>

Thank you. That was the issue. Makes perfect sense. Is now working.

— chuck
