Python function returns import module error

Hi,

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 test.py 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 https://github.com/arnauorriols/node-red-contrib-python-function/issues/8

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

@dceejay Do u have any suggestions on how we can avoid this error with libraries which were installed on the pi via the Thonny IDE?

I for some reason cannot import modules which are not available in a standard python build in raspberrypi. For example, I installed a Robinhood package called robin-stocks but it keeps saying the following

exit code: 1, Traceback (most recent call last):
File "main.py", line 1, in
import robin_stocks as RH
ImportError: No module named robin_stocks

I am fairly certain i am not running this as root. Any help would be appreciated

That is a python question... this is a Node-RED forum. I have no idea.

I was having the same issue with minimalmodbus libray. I reinstalled the libraries , both for python2 and python3. It is working fine now.

Look for any other dependent library for the library you are using. Like I had pyserial dependency. So I reinstalled it also.

@mrino1000 When you say reinstall the libraries, are you telling me to reinstall it in the raspberry pi? so basically uninstall and reinstall it on the pi again?

I wasn't sure if this issue was because the node red wasnt looking at the python default installed location for the libraries.