Running Python in Node-RED: Custom Nodes You Need
Node-RED is a powerful flow-based tool for wiring together hardware devices, APIs, and online services. When your project demands Python’s ecosystem—be it machine learning libraries, data manipulation, or quick scripting—you’ll want custom nodes that bridge Python and Node-RED seamlessly. In this article, we explore three standout community-built nodes that let you run Python code within your flows, each catering to different needs and workflows.
node-red-contrib-pythonshell
node-red-contrib-pythonshell provides a simple shell-like interface for executing Python scripts. It’s designed for speed and ease of use—ideal when you want to spin up a quick Python script and feed its output back into your Node-RED flow without fuss.
Pros:
- User-friendly setup with minimal configuration.
- High execution speed, making it great for rapid prototyping.
Cons:
- Limited options for customizing the Python runtime or handling complex dependencies.
Installation:
- Available via Node-RED’s Palette Manager or npm under
node-red-contrib-pythonshell
.
node-red-contrib-python-venv
When isolation matters—such as managing conflicting Python packages or ensuring reproducible environments—node-red-contrib-python-venv is your go-to. It runs scripts in a virtual environment, keeping your global Python setup pristine and predictable.
Pros:
- Maintains a clean global scope by encapsulating dependencies.
- Lets you target specific Python versions or library sets per node.
Cons:
- Requires extra setup to initialize and manage virtual environments.
- Slightly steeper learning curve for those new to Python venv workflows.
Installation:
- Install via Node-RED’s Palette Manager; configure the
venv
path in the node’s settings.
Pynodered
Pynodered acts like a bi-directional bridge, turning Node-RED into a full-fledged host for Python modules. Instead of just firing off scripts, it lets you expose Python functions as Node-RED nodes, enabling true code reuse and deeper integration with Python libraries.
Pros:
- Exposes Python functions directly in the Node-RED editor.
- Facilitates complex workflows by leveraging Python’s rich ecosystem.
Cons:
- Initial configuration and registration of functions can be more involved.
- May require familiarity with both Node-RED’s and Python’s packaging conventions.
Installation:
- Install
pynodered
from npm and follow the documentation to register your Python scripts as nodes.
Choosing the Right Node
- For quick scripts or one-off tasks, start with node-red-contrib-pythonshell.
- When dependency management and reproducibility are critical, opt for node-red-contrib-python-venv.
- If you need deep integration and want to surface Python code as reusable nodes, go with Pynodered.
Each of these nodes unlocks a different dimension of Python within Node-RED. Try them out, benchmark them against your use case, and lean into the one that accelerates your development.
Looking ahead, we’ll dive into best practices for debugging Python flows in Node-RED, performance tuning tips, and how to deploy Python-powered Node-RED solutions at scale. Stay tuned for more on leveraging the full potential of this hybrid ecosystem.
Introducing the Python Bridge for Node-RED
Alongside the Python Shell, virtual-env, and Pynodered nodes discussed earlier, there’s a fourth option—often called the Python Bridge—that offers a persistent, high-performance connection to a live Python interpreter. Instead of spawning a fresh process on each message, it keeps a single Python session alive and pipes commands back and forth, cutting startup overhead and unlocking more interactive workflows.
node-red-contrib-python-bridge
node-red-contrib-python-bridge is a community-built Node-RED node that leverages the python-bridge NPM library under the hood. It launches one Python process when your flows deploy, then uses JSON-RPC over stdio to execute snippets, call functions, and import modules on the fly.
Pros:
- Maintains a persistent Python REPL for ultra-low latency
- Seamlessly pass variables and complex data structures between Node-RED and Python
- Supports async/await patterns in Node.js code to coordinate with Python calls
Cons:
- A long-running Python process means you need to manage state carefully
- If the Python interpreter crashes, all dependent flows will hang until you redeploy
- Requires a compatible Python version and matching library dependencies
Installation:
- From your project folder or in Node-RED’s root directory run:
npm install node-red-contrib-python-bridge python-bridge
- Restart Node-RED or use the Palette Manager to install “python-bridge”.
Configuration:
- Python Path: Point to your
python3
executable (virtual-env paths are supported) - Init Script: (Optional) Supply a block of code to run once at startup—ideal for imports or global setup
- Timeout: Define how long Node-RED waits for a Python result before flagging an error
Quick Start Example
- Drag the Python Bridge node onto your flow.
- Set the Python Path to your interpreter (e.g.,
/usr/bin/python3
). - In the node’s Function field enter:
def multiply(a, b): return a * b
- Wire an Inject node sending
{ "a": 6, "b": 7 }
to the Bridge. - In a downstream Function node, call:
// within a Node-RED function node msg.payload = await python.call('multiply', msg.payload.a, msg.payload.b); return msg;
- Attach a Debug node to see
42
pop out in your sidebar.
When to Choose the Python Bridge
- You need sub-second responses and can’t tolerate process-spawn delays.
- Your flows rely on interactive Python modules (e.g., TensorFlow sessions, open sockets).
- You want to define Python functions once and reuse them across many messages with minimal overhead.
If you prefer stateless execution or need strict isolation per message, stick with the Shell or venv nodes. But for long-running analytics or real-time workflows—like streaming data from sensors into a trained ML model—the Python Bridge is the fastest, most flexible integration available today.
Comparing node-red-contrib-python-bridge and Pynodered
When you need to blend Python’s power into your Node-RED flows, both node-red-contrib-python-bridge and Pynodered look attractive. At a glance, each lets you call Python code—but under the hood, they target different developer workflows, performance profiles, and integration styles.
1. Integration Model
node-red-contrib-python-bridge
Launches a single, persistent Python interpreter at flow deployment. Node-RED talks to it over stdio via JSON-RPC. Your flows send commands into that live session and receive results back immediately.Pynodered
Scans your Python modules (usually marked with decorators) at startup and automatically generates native Node-RED nodes for each exported function. When a flow triggers one of those nodes, it invokes the corresponding Python function, then returns the result.
2. Developer Workflow
Bridge
- Write inline scripts or load external
.py
files in the Bridge node’s “init script.” - Call any function dynamically using
await python.call('funcName', args…)
inside a Function node. - Manage imports, globals, and state manually within that one interpreter session.
- Write inline scripts or load external
Pynodered
- Decorate your Python functions (for example with
@pynodered.node
) in a module. - Install or configure Pynodered to point at your module directory.
- Each decorated function appears as a drag-and-drop Node-RED node, complete with configurable inputs, outputs, and properties.
- Decorate your Python functions (for example with
3. State and Performance
Criterion | Python Bridge | Pynodered |
---|---|---|
Interpreter lifecycle | One long-running process | May spin up short-lived workers per node invocation or reuse pools, depending on setup |
State persistence | Variables, sockets, ML models stay loaded | Minimal persistent state unless you code it in; functions are stateless by default |
Latency | Millisecond-level, no spawn overhead | Slight startup overhead when invoking each node, but optimized for repeated calls |
Resource isolation | Shared across all calls—handle with care | Each function node can be sandboxed separately |
4. When to Choose Which
Pick node-red-contrib-python-bridge if you need
- Sub-second, interactive workflows (e.g., real-time sensor processing).
- A REPL-style session where you load models once and call them many times.
Pick Pynodered if you want
- A declarative, Node-RED–native palette of Python-powered nodes.
- Clear separation between each function’s configuration and Node-RED’s UI, without juggling JSON-RPC calls.
Both tools unlock Python’s ecosystem in Node-RED, but your choice hinges on whether you prefer a live Python REPL (Bridge) or a more Node-RED-centric, function-as-node approach (Pynodered).