Python3-function issue

When using python3-function node, I'm always seeing the error below when running any code. For testing purposes, I just used the line "print("test")" in my python3-function node.

I'd like to know if anyone has dealed with this issue and how to solve it.

Can you export your flow and post it here? I had to look at the python3-function code a bit ago, and this traceback you’re seeing is coming from the node itself. Either the code inserted is causing problems, or the message object that is coming in. What it does is that it serialises it to JSON, then on the python side deserialises it again. The error you see is on the deserialising: it’s doing encoding detection to try to figure out what it is, concludes it’s UTF-32 and fails in actually reading it that way, as it likely is not.

As @afelix pointed out, without showing the code you have written, it is impossible to help
Always try to give as much information as possible if you want a qualified response

1 Like

As I said, my python code is just "print("test"). I've exported the flow:

[
    {
        "id": "b016aa8a.b6bb98",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": ""
    },
    {
        "id": "ee96d5a9.034398",
        "type": "debug",
        "z": "b016aa8a.b6bb98",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "x": 570,
        "y": 200,
        "wires": []
    },
    {
        "id": "da89fea7.c005",
        "type": "inject",
        "z": "b016aa8a.b6bb98",
        "name": "",
        "topic": "",
        "payload": "start",
        "payloadType": "str",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 130,
        "y": 200,
        "wires": [
            [
                "40be339f.5fab5c"
            ]
        ]
    },
    {
        "id": "40be339f.5fab5c",
        "type": "python3-function",
        "z": "b016aa8a.b6bb98",
        "name": "",
        "func": "print(\"test\")",
        "outputs": 1,
        "x": 350,
        "y": 200,
        "wires": [
            [
                "ee96d5a9.034398"
            ]
        ]
    }
]

Your flow is not importable. Please read this thread and then edit your previous thread and fix the flow

1 Like

Can you check it now?

Your flow is working for me but you cannot use print in the code, it does not show anything
Use node.warn or node.send if you want to print something in the debug window

Like node.send(msg)

Yes it imports now AND on my Mac it runs fine with the caveat that the print statement sends the output to the NR log and not to msg.payload. if you use this code:

print("hello")
return("world")

you will see 'hello" in the nr log:

4 Mar 14:20:21 - [info] [python3-function:19c0125e.c6e2f6] Python function '' running on PID 12506
4 Mar 14:20:21 - [info] Started modified flows
4 Mar 14:20:24 - [info] [python3-function:19c0125e.c6e2f6] hello

and in a debug node you will see 'world' is returned
Screen Shot 2020-03-04 at 9.22.21 AM

As for why it isn't running for you, I can't help since I'm a mac user, but it might be an issue with how/where you installed python

I'm running it in a Pi3, works fine there, check that you have the latest Python3 node installed
image

It might be, once again, for running it on Windows. Python and encoding is always iffy, it's my biggest complaint I have on the language. Windows is not the best either when it comes to this, sadly. I'll see if I can start a windows system (though it might take me a week as I'm packing everything up atm) with python to see what happens there.


Is there a specific thing you're hoping to do in Python? Another user on the forum had recently issues with the same node, for what appears to be the same reasons. He switched to the pythonshell node and ran it that way. His use case was modelling a regression algorithm for machine learning, and displaying the parameters in a chart on the dashboard. While the modelling script he used could have been ported to javascript, some things are, generally, just easier in Python.

Maybe the problem really be Windows.

In my code, I'd like to implement some libraries in Python with many methods and call them in Node-red, passing an output from a method to another one as input. This way, the end-user could implement his own logic.

Thanks for now, but I would be very happy to solve my problem to use this node under Windows platform.

If it is really simple things you will write I think you may use the node. Otherwise, make yourself a favor, write your python scripts outside of NR and use them as "services" instead. Eventually put them in a Linux box. This will give you full freedom to use also multi-threading, multi-processing etc etc which will not work in the node. You can integrate easily with NR using MQTT. I have used this method for many of my "python services"

On the other hand, if it is just simple python code you will write in the node, you could most likely write it in javascript instead

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.