Search code examples
pythonnode-red

Running a Python script in Node-RED results in [Errno 2] No such file or directory


I am just starting out with Node-RED and trying to run a Python script but get the error "No such file or directory". Node-Red is running in docker on my Pi4.

python: can't open file '/home/pi/docker/nodered/data/python/bins.py': [Errno 2] No such file or directory

In node red, I'm using an inject node to run an exec with the Python command then outputting to a debug node.

I can run the script from the command line using the same command so the path is correct. This is the output:

pi@raspberrypi4:/ $ python /home/pi/docker/nodered/data/python/bins.py
I am bloody here: /home/pi/docker/nodered/data/python/bins.py

So the path is correct. What am I doing wrong that's stopping Node Red seeing the script? I've read several posts that say to the use the full path. The only other thing I can think of is that docker can't see outside of itself so would require a relative path but I've tried this as well.

Feeling a bit dim at the moment!

Many thanks,


Solution

  • The path is not correct, because it does not exist inside the container.

    The point of containers is to isolate the application from the host machine (so it runs with a known set of dependent libraries and settings).

    If you want to run the python file you will need to mount it into the container. e.g.

    docker run -d -p 1880:1880 -v /home/pi/docker/nodered/data/python/bins.py:/data/bins.py nodered/node-red
    

    You would then use the path /data/bins.py to point to the file in the exec node.

    If you have already mounted the /home/pi/docker/nodered/data into the container on /data then the same path would be used in the exec node without the need to the extra -v /home/pi/docker/nodered/data/python/bins.py:/data/bins.py to the command line.

    docker run -d -p 1880:1880 -v /home/pi/docker/nodered/data:/data nodered/node-red
    

    Path would be /data/python/bins.py

    The important thing to know is what the path is INSIDE the container, not on the host.