Error on "import explorerhat" in CGI script

I’m attempting to write a CGI script to control my explorer hat from a web page. The hat works fine with runtime scripts.

#!/usr/bin/env python
import cgi
import cgitb
cgitb.enable()
print ("Content-type: text/html\n\n")
print ("<h1>Hello World</h1>")
import explorerhat
import RPi.GPIO as GPIO # import a library

The script works fine as a runtime script and works in cgi if I remove the line “import explorerhat” however, when the import is present I get:

A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.

/usr/lib/cgi-bin/motor.py in ()
10
=> 11 import explorerhat
12 import RPi.GPIO as GPIO # import a library
13
explorerhat undefined
/usr/lib/python2.7/dist-packages/explorerhat/init.py in ()
26 exit(“This library requires the cap1xxx module\nInstall with: sudo pip install cap1xxx”)
27
=> 28 from .ads1015 import read_se_adc, adc_available
29 from .pins import ObjectCollection, AsyncWorker, StoppableThread
30
read_se_adc undefined, adc_available undefined
/usr/lib/python2.7/dist-packages/explorerhat/ads1015.py in ()
20
21 address = 0x48
=> 22 i2c = SMBus(i2c_bus_id())
23
24 REG_CONV = 0x00
i2c undefined, SMBus = None, i2c_bus_id = None
<type ‘exceptions.IOError’>: [Errno 13] Permission denied
args = (13, ‘Permission denied’)
errno = 13
filename = None
message = ''
strerror = ‘Permission denied’

I have run:
sudo chown -R www-data /usr/lib/python2.7/dist-packages
and
sudo chmod -R 777 /usr/lib/python2.7/dist-packages

Any assistance would be greatly appreciated.

I’ve never attempted to use Python CGI before- I’d probably accomplish the same thing with a Flask server to avoid that extra complexity.

However, this looks like it might be relevant (albeit you’ve already tried to address point 2):

Here are a couple of possibilities:

  • Apache (on Unix) generally runs as a different user, and with a different environment, to python from the command line. Try making a small script that just prints out sys.version and sys.prefix, and compare the result through apache and via the command line, to make sure that you’re running from the same installation of python in both environments.
  • Is Biopython installed under your home directory, or only readable just for your normal user? Again, because apache generally runs as a different user, perhaps you don’t have access to that location, so can’t import it.
  • Can you try doing import site before trying to import Biopython? Perhaps something is preventing site packages from being imported when you run through apache.

Source: Why can't python find some modules when I'm running CGI scripts from the web? - Stack Overflow

from the cgi module documentation:

for security reasons, the HTTP server executes your script as user “nobody”, without any special privileges. It can only read (write, execute) files that everybody can read (write, execute)

this is your problem right there… unless that ‘nobody’ user has access to the i2c (and gpio) group then it will fail, because the SMBus instance of class cannot be created.

how you go about sorting that out I’m not entirely sure. I guess that the cgi module creates a user on installation? if so then it should be possible to add it to those groups. If not then, opening permissions of the i2c and gpio groups to all users would be needed.

reading up on the purpose of user nobody I’d say that it would probably be frowned upon amending its own privileges, so that leaves opening up the i2c and gpio to everybody (not that does not carry its own security risk, you’ll have to consider whether that would be a concern for you).

Thanks for all of the suggestions.
I thought I would give Flask a try since it seems a much simpler solution, however I ran into issues there as well. I installed flask and ran through the first few examples in the tutorial, however when I added the “import explorerhat” statement to even the hello-flask sample it gave me an error “Warning, could not find analog or touch … Please check your i2c settings”. I think at this point I will resign myself to the fact that the explorer hat module has some features that don’t play well with other software, which is unfortunate because it’s a pretty cool tool.