Python Virtual Environments with Node-RED

Hi Forum,

I am working on a project that involves developing Node-RED nodes that can use Python libraries and virtual environments for interacting with Raspberry Pi. A similar example is node-red-node-pi-gpio, which I see uses the RPI.GPIO python library to achieve its functionality.

More precisely I want to do the following:

  • The node should run a virtual environment, in which it installs the required Python libraries.
  • The process of creating/activating the virtual environment and installing dependencies should be automated. I would like to avoid making the user manually install packages.
  • The node should then run a Python script which imports the libraries and provides functionality to the node using the libraries.

So taking inspiration from node-red-node-pi-gpio I came up with the following (perhaps hacky) way to achieve this. This is a node (we'll call it my-node) that basically uses a Python library for interacting with SPI devices.

  1. my-node's JS file initiates a child process, which calls a bash executable.
  2. The bash executable checks if a venv folder exists within the project directory. If not, it creates one and then using the newly created venv, it installs Python packages from a requirements.txt file.
  3. The bash executable then runs a Python script (using the same venv).
  4. This Python script imports the installed libraries, and interacts with an SPI device. It receives commands from and outputs data to the parent process, i.e. the JS file.

So, this does seem to be working. However, I am wondering if what I am doing is best practices, or if there are better ways to achieve this. For example, there a few issues with my approach:

  1. I tested this node on multiple Raspberry Pi units. Some did not have the pip, or dev packages installed, and thus of course, the installation of packages failed. This made me think then, not everyone is using node-red on a machine that has Python, pip, dev, or really any other number of packages whose absence could impede my-node's Python functionality. So my question is, what is the standard/expectation around using nodes that leverage Python, in terms of these dependencies? Is it reasonable to expect users to have python and other required packages pre-installed before using the node? I see for example that node-red-node-pi-gpio's README warns users they must have RPI.GPIO before using this node. I would like to avoid this.
  2. Since I would like to avoid placing the onus on the user to install packages/dependencies before using my-node, and given my solution for this seems a bit hacky, is there some way to install dependencies, packages, and create a virtual environment by simply installing my-node? For example, right now I am checking to see if the venv and potentially install packages exists every time my-node is deployed. Is there a way to have this occur when my-node is installed, perhaps by including some script to run at installation time?

Basically, is there a better/simpler way of achieving my-node's functionality?

You can't avoid forcing the user to install non nodejs pre-requisits.

Your only option is to list them and instructions for the user to follow.

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