Nodes/ComfyUI-KYNode/Eval Python Code
ComfyUI Node

Eval Python Code

A real Python interpreter living inside a node

By yorkane·Created 2 years ago·Updated 6 months ago· 10
Eval Python Code
  • pycode
  • r0_str

Don't confuse this with this pack's own KY_MathExpression. That one is a scoped evaluator - a fixed vocabulary of math functions and nothing else. This node is the opposite: a genuine "run whatever Python you type" box, described in the node's own text as an "IDE Node… that allows you to run code written in Python or Javascript directly in the node." There's no allowlist, no sandbox implied anywhere in the schema. You write real code, it runs.

What the default code tells you

The default pycode value is a small worked example: it imports re, json, os, traceback, and strftime from time, defines a function runCode() that returns a timestamped greeting string, calls it, and assigns the result to a variable named r0_str. That last part isn't incidental - the node's single output is also named r0_str, which tells you the convention: whatever variable name your code assigns its result to has to match the output socket's name for the value to actually flow out of the node. Get the variable name wrong and the output likely comes back empty or None, even if your code ran without raising an error.

The imports in the default snippet (os, in particular) are the tell for how unrestricted this actually is - this isn't a node that limits you to string or math operations, it's handing you the same Python environment ComfyUI itself runs in.

Inputs and outputs

One required input: pycode, a multiline PYCODE-typed string field, pre-filled with the example above. One output: r0_str, wildcard-typed, so whatever your code assigns to that variable comes out as-is regardless of its actual type.

Say this plainly: this is a code-execution surface

ComfyUI already treats custom nodes as arbitrary code - that's the standing tradeoff of the whole ecosystem, the same reason .safetensors won over pickle-based .ckpt files for model weights but never made the surrounding node code any safer. What's different here is that the arbitrary code isn't hidden inside a node's Python source you'd install once and forget about - it's a workflow-editable field. The actual code lives in the widget value, which means it's serialized straight into the workflow JSON itself. Anyone who shares a workflow file that uses this node is shipping you code that runs the moment you load and queue that workflow, no separate installation step, no review. Load a stranger's workflow that happens to use KY_Eval_Python and you're running their Python, on your machine, with whatever access your ComfyUI process has.

That's not a defect in the node - running arbitrary code is the entire feature - but it's worth knowing before you paste in a workflow from a random forum post or Discord channel that includes one of these.

Installing it

ComfyUI Manager: search ComfyUI-KYNode, install, restart. Or:

cd ComfyUI/custom_nodes
git clone https://github.com/yorkane/ComfyUI-KYNode

Restart ComfyUI. No extra dependencies to install for the node itself - but whatever your code imports and calls needs to already exist in ComfyUI's own Python environment, since it's running inside that process, not a separate sandbox.

Common issues

The output-variable-name convention above is the most common "why is my output empty" question - double-check your code actually assigns its final result to a variable literally named r0_str, not just returns it from a function or prints it.

A syntax error or an unhandled exception in your code is going to surface as a hard node failure, the same as any other Python error inside ComfyUI - there's no graceful partial-output fallback implied by the schema, so treat this like writing a real script, not a forgiving expression box.

And per the section above: treat any workflow you didn't write yourself that contains this node with the same caution you'd give an unfamiliar .py file someone emailed you. Read the code in the pycode field before you queue it.

CategoryKYNode/Code

Inputs (1)

NameTypeDefaultDescription
pycodePYCODEimport re, json, os, traceback from time import strftime def runCode(): nowDataTime = strftime("%Y-%m-%d %H:%M:%S") return f"Hello ComfyUI with us today {nowDataTime}!" r0_str = runCode()

Outputs (1)

NameTypeDescription
r0_str*