Permissions issue when running a python script

that makes sense,

I tried it, first command returns ‘openhab is already a member of gpio’, second is the same error as shown in the first post.
Output below…

pi@raspberrypi:~ $ sudo adduser openhab gpio
The user openhab' is already a member ofgpio’.

pi@raspberrypi:~/sensor $ sudo -u openhab python
Traceback (most recent call last):
File “”, line 11, in
File “build/bdist.linux-armv7l/egg/Adafruit_GPIO/”, line 42, in init
IOError: [Errno 13] Permission denied

I have a feeling the user openhab isnt given permission to access the file where the SPI library is, in this line??
File “build/bdist.linux-armv7l/egg/Adafruit_GPIO/”, line 42, in init
IOError: [Errno 13] Permission denied

Im probably wrong but Im not experienced diagnosing these errors.


@steven_191 so i looked into my rpi, and listet the groups which are allready defined.

pi@raspberrypi:~ $ groups
pi adm dialout cdrom sudo audio video plugdev games users input netdev gpio i2c spi

As we can see there is a group for spi, which you try to acces.
Maybe it helps to add openhab to this group and grant acces to spi interface.

sudo adduser openhab spi

Maybe this solves the problem.

If the file you want to execute is not permitted to be executed by openhab you have to change the permission of the file.

ps. please use code fences for the output to increase readability.

I have added openhab to the spi group but still getting the same error. I cant even find the file in the error in order to try to change permissions so Im stuck again.
Here are my groups.

pi@raspberrypi:/etc/openhab2 $ groups
pi adm dialout cdrom sudo audio video plugdev games users input netdev gpio i2c spi
pi@raspberrypi:/etc/openhab2 $ groups openhab
openhab : openhab spi gpio

I dont see any indication in the error of a group permission, only that file which I believe is related to the SPI library.

I have also tried to add openhab to the sudo group but this had no effect either.


I just set up an spi python test.

first i added the user openhab to the spi group and ran the spi test from the rasberry foundation page to test if it works.

sudo adduser openhab spi

downloaded the exaple and run it as openhab.

sudo -u openhab ./spidev_test -D /dev/spidev0.0
spi mode: 0
bits per word: 8
max speed: 500000 Hz (500 KHz)


So this works.

Then i downloaded the adafruit gpio and installed it, which is not neccessary to get this test running but i had peek into the code. Also had a look into this manual.

I set up an example to trassmit some spi data.
#!/usr/bin/env python

import spidev
import time

spi = spidev.SpiDev(),0)
spi.max_speed_hz =7629

resp = spi.xfer2([0xaa])
print resp[0]

Made the skript executable

 chmod +x

And run it as user openhab

sudo -u openhab ./

The output of my logic analyzer suggest that it works. :wink:

As the postet error line in the

is the opening of the spi device, device)

And in my test i also opened the spi device, this steps above could help you getting it running.

theres a lot there that I dont understand.

I copied my existing python program in the same directory under a different name, tested it, I made it executable and it give me errors, but when called with python it still runs fine. No idea whats happening there.

pi@raspberrypi:~/sensor $ python
pi@raspberrypi:~/sensor $ ./
./ line 1: import: command not found
./ line 2: import: command not found
./ line 3: import: command not found
./ line 4: $'\r': command not found
./ line 7: $'\r': command not found
./ line 8: syntax error near unexpected token `('
'/ line 8: `INDEX=int(channel)

The specific Adafruit library im using is for the MCP3008. Not sure if its any different to what you found.

Thanks for your help so far but this is beyond my knowledge.

@steven_191 can you run my example?

Do you have the shebang at the first line of your skript?

#!/usr/bin/env python

can you post the content of your ?

Just realised I didnt have the bin/bash whatever at the start of the file but now struggling to get that right.

I tried the shebang you mention and various others but non work, I tried


but returned nothing which is probably why??

I have only been calling the scripts with python and used that in openhab too in the exec commands and it did work fine.

the script is here

import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008
import time

#set up channel ref here!!





def getRes():
        if adc0>=1023 or adc0<=1:
                return 1024
        return res


for i in xrange(0,len(table)-2,2):
        if res0<=table[i+1] and res0>=table[i+3]:


@steven_191 does the python interpreter launch if you type

/usr/bin/env python

if yes then my shebang should work. because the path to python is in /user/bin/env.

If not, just type that into the shell.

which python

and use this path as shebang, this schould be the path to your python executable. This will work on your system, but will or will not work with other systems. Thats why the /user/bin/env is used it makes sure that no matter which system you use the path to python is set correctly dependend on your environment.

And it is not the same as $PYTHONPATH. PYTHONPATH is where python when it runs will look into to find references.

I can call up the interpretor with

/usr/bin/env python

But in the script I have

#!/usr/bin/env python

and get this error

pi@raspberrypi:~/sensor $ ./
/usr/bin/env: âpython\râ: No such file or directory

I tried again with a script that only create an instance of the MCP3008 input channel and it failed with the same error.
But it runs fine if I just call the script with python.
I think this is some other issue though from the original problem. I can call the script from openhab with the python prefix and it does work for script that arent using the SPI side.

I would guess it’s a file encoding problem. How did you create the file? Do you edit the file from a Windows PC?

Try to create a file with the OS on the rpi and write the line, do not copy paste it.

All written locally on the raspberry pi, although I did you do it by opening the file in nano.

That being said, everything works fine, no unknowns errors, expect this. So if it was encoding, I would have thought something would have already come up as a problem?

I havent copied or pasted anything in this script.

considering the format of the shebang could it be that â has replaced a space and \râ has replaced the carriage return?

I checked the link you posted and this is the result of the file. I dont know if there is an issue with that?

pi@raspberrypi:~/sensor $ file -bi
text/x-python; charset=us-ascii

@steven_191 so you edit all files in Linux and also downloaded or git cloned the Adafruit sources on the rpi? No windows at all? Encoding of your seems ok, can you check the imported files for their encoding!

import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008

what does this commands return?

pi@raspberrypi:~$ which python

pi@raspberrypi:~$ python -V
Python 2.7.13

So Running my example works properly?
Only when you try to create an instance of mcp=Adafruit_MCP3008 you have this errors?

Hopefully answers to all of your questions here.

pi@raspberrypi:/usr/local/lib/python2.7/dist-packages $ ls
Adafruit_GPIO-1.0.3-py2.7.egg     setuptools-3.5.1-py2.7.egg
Adafruit_MCP3008-1.0.2-py2.7.egg  setuptools.pth
Adafruit_PureIO-0.2.1-py2.7.egg   spidev-3.2-py2.7-linux-armv7l.egg

pi@raspberrypi:/usr/local/lib/python2.7/dist-packages $ file -bi Adafruit_GPIO-1.0.3-py2.7.egg
application/zip; charset=binary

pi@raspberrypi:/usr/local/lib/python2.7/dist-packages $ file -bi Adafruit_MCP3008-1.0.2-py2.7.egg
application/zip; charset=binary

pi@raspberrypi:~ $ which python
pi@raspberrypi:~ $ python -V
Python 2.7.13

after noticing that the python path wasnt actually /usr/bin/env like I thought it was, I used the path above but then got this error

pi@raspberrypi:~/sensor $ ./
': [Errno 2] No such file or directoryon

So I guess thats wrong too.

I remember banging my head for hours trying to get a Lightify script running here:

And what worked for me was needing to move/copy the installed modules over to the phyton2.7/dist-packages. Can you confirm where your pip packages installed to and if the user OpenHab has access to that folder?

@steven_191 so i just tried to run the Adafruit_Python_MCP3008.

I got the files from git and installed it as described

Then i had a look to the files and the permissions

pi@raspberrypi:/usr/local/lib/python2.7/dist-packages $ ls -l
total 100
-rw-r--r-- 1 root staff 76795 Nov  1 08:20 Adafruit_GPIO-1.0.3-py2.7.egg
-rw-r--r-- 1 root staff  5073 Nov  7 23:56 Adafruit_MCP3008-1.0.2-py2.7.egg
-rw-r--r-- 1 root root   9388 Nov  1 08:20 Adafruit_PureIO-0.2.1-py2.7.egg
-rw-r--r-- 1 root staff   101 Nov  7 23:56 easy-install.pth

I changed them to user root and also to be executable

pi@raspberrypi:~/usr/local/lib/python2.7 $ sudo chown -R root:root dist-packages/
pi@raspberrypi:~/usr/local/lib/python2.7 $ sudo chmod -R +x dist-packages/
pi@raspberrypi:~ $ ls -l /usr/local/lib/python2.7/dist-packages/
total 100
-rwxr-xr-x 1 root root 76795 Nov  1 08:20 Adafruit_GPIO-1.0.3-py2.7.egg
-rwxr-xr-x 1 root root  5073 Nov  7 23:56 Adafruit_MCP3008-1.0.2-py2.7.egg
-rwxr-xr-x 1 root root  9388 Nov  1 08:20 Adafruit_PureIO-0.2.1-py2.7.egg
-rwxr-xr-x 1 root root   101 Nov  7 23:56 easy-install.pth

I had to change the to initialize temp to avoid errors and added the shebang. I forced it to use python2.7.

#!/usr/bin/env python2.7
# Simple example of reading the MCP3008 analog input channels using its
# differential mode.  Will print the difference of channel 0 and 1.
# Author: Tony DiCola
# License: Public Domain
import time

# Import SPI library (for hardware SPI) and MCP3008 library.
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008

# Software SPI configuration:
# CLK  = 18
# MISO = 23
# MOSI = 24
# CS   = 25
# mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI)

# Hardware SPI configuration:
SPI_PORT   = 0
mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))

print('Press Ctrl-C to quit...')
while True:
    # Grab the difference between channel 0 and 1 (i.e. channel 0 minus 1).
    # Note you can specify any value in 0-7 to grab other differences:
    #  - 0: Return channel 0 minus channel 1
    #  - 1: Return channel 1 minus channel 0
    #  - 2: Return channel 2 minus channel 3
    #  - 3: Return channel 3 minus channel 2
    #  - 4: Return channel 4 minus channel 5
    #  - 5: Return channel 5 minus channel 4
    #  - 6: Return channel 6 minus channel 7
    #  - 7: Return channel 7 minus channel 6
    value = mcp.read_adc_difference(0)
    print('Channel 0 minus 1: {0}'.format(value))

And then execute it.

pi@raspberrypi:~/Adafruit_Python_MCP3008/examples $ python
Press Ctrl-C to quit...
Channel 0 minus 1: 0

make it executeable

pi@raspberrypi:~/Adafruit_Python_MCP3008/examples $ sudo chmod -x
pi@raspberrypi:~/Adafruit_Python_MCP3008/examples $ ./
Press Ctrl-C to quit...
Channel 0 minus 1: 0

Also as openhab

pi@raspberrypi:~/Adafruit_Python_MCP3008/examples $ sudo -u openhab ./
Press Ctrl-C to quit...
Channel 0 minus 1: 0

If reproducing this steps does not work for you, you should think about a clean installation as it seems that there is somthing wrong.
And if you got the example running you can think about testing your and find out what is wrong with that.

I did two things,

I changed the owner of the Adafruit files to root. I actually only had the MCP3008 file as ‘staff’ and the GPIO was already ‘root’ unlike in your post before.

Next I copied the script to /usr/local/ because I thought if thats where the packages are, and thats where the script runs from, surely thats a good thing right?

Added the thing and item and its reading fine now. Checked with both String and Number channels on the thing but only the string output is working.
Im not sure if the number output needs to work but if I can use that value as a number in the rules then it wont be a problem.

I believe it was moving the file to the location /usr/local and created a new directory there to store further scripts. I attempted to set up a thing with the path back to the original location but it gave me another error even thought the owner of the packages was changed.

Glad to finally have this sorted. Next onto the rules to see if that works.


I already had files in the location you mention so instead Ive moved my scripts to the same location and I think that is the resolution.

I am not sure that this is the right channel for my issue. Please advice if wrong.
Many years ago I did an openhab2 application but now I want to write a personal tutorial so I started from the beginning choosing a simple project.
But i have a problem with the Exec binding that should runs a python script. it put the script in /etc/openha2/scripts/…
[07:08:17] root@openhab:/home/openhabian# chmod -R +x /etc/openhab2/scripts
but anything in the script importing anything produce an error
I guess is a matter of permission (also “cd” produce the same error)
ile “/etc/openhab2/scripts/”, line 10, in

from rpi_rf import RFDevice

ImportError: No module named rpi_rf

please help me I do not know what to do

Sounds like a Python include problem. As the exec binding will have different environment as you when executing the script. Make sure executing the script as user openhab runs.

Make sure the sys.path contains the required library. Make sure that the shebang selects the correct Python installation. You might have the library installed only for one.