Get the error from paython file

Having said that it works perfectly fine for me on my Linux desktop.

Can you send over your workflow? I suspect there is a bit more to this that we are not seeing in the screenshot.

I take it from MutedJam
{
“nodes”: [
{
“parameters”: {},
“name”: “Start”,
“type”: “n8n-nodes-base.start”,
“typeVersion”: 1,
“position”: [
250,
300
]
},
{
“parameters”: {
“conditions”: {
“number”: [
{
“value1”: “={{$json[“exitCode”]}}”,
“operation”: “equal”
}
]
}
},
“name”: “Success?”,
“type”: “n8n-nodes-base.if”,
“typeVersion”: 1,
“position”: [
650,
300
]
},
{
“parameters”: {
“command”: “python3 -c ‘\nimport sys\nprint(“bye”)\nsys.exit(1)\n’”
},
“name”: “Execute Python Script”,
“type”: “n8n-nodes-base.executeCommand”,
“typeVersion”: 1,
“position”: [
450,
300
],
“continueOnFail”: true
},
{
“parameters”: {},
“name”: “Success Path”,
“type”: “n8n-nodes-base.noOp”,
“typeVersion”: 1,
“position”: [
850,
200
]
},
{
“parameters”: {},
“name”: “Error Path”,
“type”: “n8n-nodes-base.noOp”,
“typeVersion”: 1,
“position”: [
850,
400
]
}
],
“connections”: {
“Start”: {
“main”: [
[
{
“node”: “Execute Python Script”,
“type”: “main”,
“index”: 0
}
]
]
},
“Success?”: {
“main”: [
[
{
“node”: “Success Path”,
“type”: “main”,
“index”: 0
}
],
[
{
“node”: “Error Path”,
“type”: “main”,
“index”: 0
}
]
]
},
“Execute Python Script”: {
“main”: [
[
{
“node”: “Success?”,
“type”: “main”,
“index”: 0
}
]
]
}
}
}

It seems like Windows doesn’t like multiline commands so I’d suggest giving @jon’s approach a go here (e.g. instead of pasting the Python script in n8n, put it in a separate file and have python run that file like python C:\Users\Tom\hello.py).

That’s not an n8n restriction though but a Windows one though which the Execute Command node cannot overcome.


but if I remove the sys.exit, it’s all good

ok, all the flow is working, when I use the sys.exit(1) it’s going to the path of false, and the when sys.exit(0) it’s going to true
thanks for all the help, if you will know any solution to write python code in the n8n node (in windows system) I will glad to hear

1 Like

but I still want to get a message with the error code, now he goes to the path of false when sys.exit(1) and he go to path false because he doesn’t found value from previous node

So this will be a bit tricky because of how the error response looks like. But assuming your Python script writes error messages to stderr like so:

import sys
print("my error", file=sys.stderr)
sys.exit(1)

And you call it in the Execute Command node like so:

python C:\Users\Tom\hello.py

Your result item would have an error key with a value of Command failed: python C:\Users\Tom\hello.py my error. You could then for example extract the error message after the command using JavaScript’s .split():

Example Workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{$json[\"exitCode\"]}}",
              "operation": "equal"
            }
          ]
        }
      },
      "name": "Success?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "command": "python C:\\Users\\Tom\\hello.py"
      },
      "name": "Execute Python Script",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        450,
        300
      ],
      "retryOnFail": true,
      "waitBetweenTries": 5000,
      "continueOnFail": true
    },
    {
      "parameters": {},
      "name": "Success Path",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        850,
        200
      ]
    },
    {
      "parameters": {},
      "name": "Error Path",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        850,
        400
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "error_message",
              "value": "={{$json[\"error\"].split($node[\"Execute Python Script\"].parameter[\"command\"])[1].trim()}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Get Error Message",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1050,
        400
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Execute Python Script",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Success?": {
      "main": [
        [
          {
            "node": "Success Path",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Error Path",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute Python Script": {
      "main": [
        [
          {
            "node": "Success?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Error Path": {
      "main": [
        [
          {
            "node": "Get Error Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Might need some minor adjustments for your specific situation and script but I tested this on Windows and it works.

thanks, it was very helpful