Permissions issue when running a python script


For anyone planning on reading on, here is the quick answer. The openhab user could access the user space of the pi user where I was writing the scripts.
The MCP3008 library was installed in /usr/local/blah blah blah
I added a folder and put my scripts in /usr/local/newFolder
As the openhab user and pi have access to that directory it can call the scripts with no issues.

Im not exactly a pro when it comes to linux or openhab so Im trying to get all my info right here.

I installed openhab2, set up a few things and items and sitemap following the tutorial, no problems there.

I have a python script to call an ADC over SPI and calculate a temperature value.

This works fine when run from the pi user without sudo.

I set up the exec binding but having various troubles. Lots or trawling through websites and forums (including this one) but I think I might have found the problem, but my lack of experience means I cant be sure/resolve it.

I set up a simple python script ~ print(“101”)

set up the thing and item string output and it gets back the value no problem.

When calling the python script that requires the SPI bus I get this error on the paperUI control webpage

Traceback (most recent call last): File “/home/pi/sensor/”, line 11, in mcp=Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT,SPI_DEVICE)) File “build/bdist.linux-armv7l/egg/Adafruit_GPIO/”, line 42, in __init__IOError: [Errno 13] Permission deniedTraceback (most recent call last): File “/home/pi/sensor/”, line 11, in mcp=Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT,SPI_DEVICE)) File “build/bdist.linux-armv7l/egg/Adafruit_GPIO/”, line 42, in __init__IOError: [Errno 13] Permission denied

If im reading it right, I think the problem is that the user that is running openhab (as far as I can tell, ‘openhab’ is the user, but there is no user account for this, not like the ‘pi’ default account??), is not given permission to access the library for the SPI bus or the SPI bus itself?
the python script is located in the ‘pi’ home directory so I dont know if that has anything to do with it, I used an Adafruit program that came with the library in order to install the library so I dont know if its installed with permission for ‘pi’ to use but no one else?
If its just user permission can I save my python script in some directory that openhab can call without permission errors?

Im really stuck because the other exec things work fine, but the one I want (with SPI access) wont.


sudo adduser openhab gpio
sudo -u openhab <your Skript>

Maybe this will already help. I don’t know if you need more privileges then gpio.
First we add the user openHAB to the group gpio. Then we try to call the Skript as the user openHAB. If this works it should work from within openHAB.

I’ll give this a shot but before I just have a few questions.

  1. is the openhab user created during the install? Because I wasn’t able to log into it

  2. if so, the commands you suggested will give the existing openhab user access to GPIO and then permission to run the script? Is that because it’s in another user’s directory?

  3. if the openhab user doesn’t exist then how would creating a user call openhab link to the openhab service running in order to give it access to anything? (this is probably a useless question as I’m sure the user exists but I can’t log into it)

I’ll try those commands soon and update. Thanks

When you install openHAB the user openhab should be created.

As describes above the first line gives the user openhab permission to use the gpio by adding him to the group gpio. The second line will execute the Skript as user openhab, not as the user you are logged in, to test if it works.

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.