Python function returns import module error



I'm testing the python function node with an inject node that returns JSON data.
The python function is simple :

return msg

but when I deploy my flow, on the debug panel "No module named json" error occurred.

I also tried with running a python script with exec node, the script is working fine when I launched on command line but same error occurs when launching with node red.
I am running Python 2.7.3



what user is Node-RED running as ? does it have access to the same environment as your command line user ?



Node-red is running as root, so I guess it have access to the same environment.
If I exec the script with exec node :

#!/usr/bin/env python
import sys

print sys.argv[1]

it works but when I add import json it doesn't.



Is there a reason you are running Node-RED as root?

Are you using node-red-contrib-python-function ??
If so it is known not to work with NR v0.20.x see



Thanks for your reply.

There is no particular reason to run node-red as root, do you think is that what causes the error?
Because my final python script aims to write on serial port so I guess it will need root privileges
Yes I'm using node-red-contrib-python-function but Node-red version is 0.16.2



Running as root is not recommended.

So running as a normal user is something I would try and something you should be doing anyway.

You do not need to run as root to access the serial port from Node-RED or from python. The user needs to be a member of the 'dialout' group.



Ok thanks I created a non root user and then restart node-red with this user, retry with python function and looks to work fine! :slight_smile:

1 Like