# Python function returns import module error

**URL:** https://discourse.nodered.org/t/python-function-returns-import-module-error/9683
**Category:** General
**Created:** [2 April 2019 08:48 UTC](https://discourse.nodered.org/t/python-function-returns-import-module-error/9683 "2019-04-02T08:48:36Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![elisascheer](https://avatars.discourse-cdn.com/v4/letter/e/71c47a/32.png) [@elisascheer](https://discourse.nodered.org/u/elisascheer)
#### Post date: [2 April 2019 08:48 UTC](https://discourse.nodered.org/t/python-function-returns-import-module-error/9683/1 "2019-04-02T08:48:36Z")

</div>

Hi,

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

```auto
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

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [2 April 2019 09:01 UTC](https://discourse.nodered.org/t/python-function-returns-import-module-error/9683/2 "2019-04-02T09:01:09Z")

</div>

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

---

<div class="post-metadata">

### Author: ![elisascheer](https://avatars.discourse-cdn.com/v4/letter/e/71c47a/32.png) [@elisascheer](https://discourse.nodered.org/u/elisascheer)
#### Post date: [2 April 2019 09:07 UTC](https://discourse.nodered.org/t/python-function-returns-import-module-error/9683/3 "2019-04-02T09:07:26Z")

</div>

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 :

```auto

#!/usr/bin/env python
import sys

print sys.argv[1]

```

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

---

<div class="post-metadata">

### Author: ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)
#### Post date: [2 April 2019 09:27 UTC](https://discourse.nodered.org/t/python-function-returns-import-module-error/9683/4 "2019-04-02T09:27:57Z")

</div>

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

---

<div class="post-metadata">

### Author: ![elisascheer](https://avatars.discourse-cdn.com/v4/letter/e/71c47a/32.png) [@elisascheer](https://discourse.nodered.org/u/elisascheer)
#### Post date: [2 April 2019 09:30 UTC](https://discourse.nodered.org/t/python-function-returns-import-module-error/9683/5 "2019-04-02T09:30:45Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)
#### Post date: [2 April 2019 09:44 UTC](https://discourse.nodered.org/t/python-function-returns-import-module-error/9683/6 "2019-04-02T09:44:15Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![elisascheer](https://avatars.discourse-cdn.com/v4/letter/e/71c47a/32.png) [@elisascheer](https://discourse.nodered.org/u/elisascheer)
#### Post date: [2 April 2019 09:56 UTC](https://discourse.nodered.org/t/python-function-returns-import-module-error/9683/7 "2019-04-02T09:56:43Z")

</div>

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! 🙂

---

<div class="post-metadata">

### Author: ![kvramji](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kvramji/32/59005_2.png) [@kvramji](https://discourse.nodered.org/u/kvramji)
#### Post date: [16 July 2020 09:01 UTC](https://discourse.nodered.org/t/python-function-returns-import-module-error/9683/8 "2020-07-16T09:01:49Z")

</div>

@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

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [16 July 2020 09:52 UTC](https://discourse.nodered.org/t/python-function-returns-import-module-error/9683/9 "2020-07-16T09:52:15Z")

</div>

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

---

<div class="post-metadata">

### Author: ![mrino1000](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@mrino1000](https://discourse.nodered.org/u/mrino1000)
#### Post date: [1 August 2020 12:52 UTC](https://discourse.nodered.org/t/python-function-returns-import-module-error/9683/10 "2020-08-01T12:52:56Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![kvramji](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kvramji/32/59005_2.png) [@kvramji](https://discourse.nodered.org/u/kvramji)
#### Post date: [3 August 2020 05:49 UTC](https://discourse.nodered.org/t/python-function-returns-import-module-error/9683/11 "2020-08-03T05:49:39Z")

</div>

@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.
