# Script python in java

**URL:** <https://forum.inductiveautomation.com/t/script-python-in-java/69623>\
**Category:** Module Development\
**Tags:** scripting\
**Created:** [February 9, 2023, 1:48pm UTC](https://forum.inductiveautomation.com/t/script-python-in-java/69623 "2023-02-09T13:48:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sebastian\_Munoz\_Vasq](https://avatars.discourse-cdn.com/v4/letter/s/ebca7d/32.png) [@Sebastian\_Munoz\_Vasq](https://forum.inductiveautomation.com/u/Sebastian_Munoz_Vasq)\
**Post date:** [February 9, 2023, 1:48pm UTC](https://forum.inductiveautomation.com/t/script-python-in-java/69623/1 "2023-02-09T13:48:51Z")

</div>

hello, I am working on running a phynton code in a java sdk module, first I wanted to try with a simpler function as I show below and I manage to extract the date

```
protected void testImpl() throws Exception {
    GatewayContext context = GatewayHook.getGatewayContext();
    ScriptManager scriptManager = context.getScriptManager();

    PyStringMap a = scriptManager.createLocalsMap();

    try {
        scriptManager.runCode("a = system.date.now()", a, "");

        logger.info("a: " + a.values().get(0));

    }catch (JythonExecException e){
        e.printStackTrace();
    }
}

```

then, I wanted to try building a dataset but I haven't been able to get the code to work.

```
    ScriptManager scriptManager = context.getScriptManager();
    PyStringMap a = scriptManager.createLocalsMap();
    
    //String code = "ds11 = system.dataset.toDataSet(" + columnName + "," + dsData + ")";
    //String code = "a = system.dataset.toDataSet([a,b],[[12,12],[11,10]])";
    //logger.info("code: " + code);
    try {
        scriptManager.runCode("a = system.dataset.toDataSet([a,b],[[12,12],[11,10]])", a,"");
        logger.info("ds11: " + a);
        logger.info("ds11: " + a.values().get(0));
        
    }catch (JythonExecException e){
        e.printStackTrace();
    }

```

---

<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:** [February 9, 2023, 1:53pm UTC](https://forum.inductiveautomation.com/t/script-python-in-java/69623/2 "2023-02-09T13:53:52Z")

</div>

> [@Sebastian\_Munoz\_Vasq](#):
>
> `a.values().get(0)`

Why are you using this construct? Use the methods of `PyStringMap` to get by name. Probably [this one](https://www.javadoc.io/static/org.python/jython-standalone/2.7.2/org/python/core/PyStringMap.html#Z:Z __finditem__ -java.lang.String-), but note the warning. (Java string constants are automatically interned, fwiw.)

---

<div class="post-metadata">

**Author:** ![Sebastian\_Munoz\_Vasq](https://avatars.discourse-cdn.com/v4/letter/s/ebca7d/32.png) [@Sebastian\_Munoz\_Vasq](https://forum.inductiveautomation.com/u/Sebastian_Munoz_Vasq)\
**Post date:** [February 9, 2023, 2:06pm UTC](https://forum.inductiveautomation.com/t/script-python-in-java/69623/3 "2023-02-09T14:06:40Z")

</div>

I'm using it for testing, but how would it be the way you mention?

---

<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:** [February 9, 2023, 2:07pm UTC](https://forum.inductiveautomation.com/t/script-python-in-java/69623/4 "2023-02-09T14:07:10Z")

</div>

`a. __finditem__ ("a")`

---

<div class="post-metadata">

**Author:** ![Sebastian\_Munoz\_Vasq](https://avatars.discourse-cdn.com/v4/letter/s/ebca7d/32.png) [@Sebastian\_Munoz\_Vasq](https://forum.inductiveautomation.com/u/Sebastian_Munoz_Vasq)\
**Post date:** [February 9, 2023, 2:44pm UTC](https://forum.inductiveautomation.com/t/script-python-in-java/69623/5 "2023-02-09T14:44:27Z")

</div>

ok, I understand, I was able to extract the date that way, however the dataset does not seem to be built.

---

<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:** [February 9, 2023, 2:56pm UTC](https://forum.inductiveautomation.com/t/script-python-in-java/69623/6 "2023-02-09T14:56:12Z")

</div>

Probably because your header names are non-existing variable names instead of strings.

Consider not using the same variable names for multiple purposes. It makes troubleshooting a nightmare.

---

<div class="post-metadata">

**Author:** ![Sebastian\_Munoz\_Vasq](https://avatars.discourse-cdn.com/v4/letter/s/ebca7d/32.png) [@Sebastian\_Munoz\_Vasq](https://forum.inductiveautomation.com/u/Sebastian_Munoz_Vasq)\
**Post date:** [February 9, 2023, 4:00pm UTC](https://forum.inductiveautomation.com/t/script-python-in-java/69623/7 "2023-02-09T16:00:36Z")

</div>

correct, by changing the instruction to this I managed to build the dataset and it has solved my problem, thank you very much.

```
        scriptManager.runCode("ds11 = system.dataset.toDataSet(['a','b'],[[10,11],[12,13]])", stringMap,"");

```

Now I would like to take the opportunity to ask another question that has to do with the same thing, I have a historical query, which I receive in a List \< String \> the header and in a List \<List\< double \>\> the data, but I get an error when building the dataset, what could you recommend?

---

<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:** [February 9, 2023, 4:06pm UTC](https://forum.inductiveautomation.com/t/script-python-in-java/69623/8 "2023-02-09T16:06:02Z")

</div>

Seems like you are trying to use java types in your python. Use `Py.javaToPy()` and `Py.pyToJava()` to move data back and forth.

And you really should not be using jython to build your datasets for java. Use the `DatasetBuilder` class directly.

Using `ScriptManager.runCode()` or `.runFunction()` have substantial overhead. Don't use jython in your module for anything that isn't user-supplied.
