1

I'm trying to run a python script using the adafruit_bme280 library to read a couple of sensors and display the data on a flask-generated page. I've gotten it to work outside of a venv virtual environment without using sudo, however as soon as I try to run it from within one I get the following error.

Traceback (most recent call last):
  File "site.py", line 63, in <module>
    i2c = busio.I2C(board.SCL, board.SDA)
  File "/home/pav/venv/lib/python3.5/site-packages/busio.py", line 16, in __init__
    self.init(scl, sda, frequency)
  File "/home/pav/venv/lib/python3.5/site-packages/busio.py", line 27, in init
    self._i2c = _I2C(portId, mode=_I2C.MASTER, baudrate=frequency)
  File "/home/pav/venv/lib/python3.5/site-packages/adafruit_blinka/microcontroller/generic_l
inux/i2c.py", line 20, in __init__

    self._i2c_bus = smbus.SMBus(bus_num)
  File "/home/pav/venv/lib/python3.5/site-packages/Adafruit_PureIO/smbus.py", line 105, in _
_init__
    self.open(bus)
  File "/home/pav/venv/lib/python3.5/site-packages/Adafruit_PureIO/smbus.py", line 130, in o
pen
    self._device = open('/dev/i2c-{0}'.format(bus), 'r+b', buffering=0)
PermissionError: [Errno 13] Permission denied: '/dev/i2c-1'

Running i2cdetect -y 1 returns Error: Could not open file /dev/i2c-1: Permission denied Run as root? as expected. Running it using sudo works.

So far, I've tried adding my user name (uname) to the group i2c using sudo adduser uname i2c, but that hasn't had any visible effect at all. I reckon I should be able to allow my user to access i2c without using sudo but I have no idea how.

  • What user is the flask script running as? You need to add whatever is actually running the script to the I2C group. – joan May 17 at 11:32
  • I'm running '$ python3 script.py' so I assume it's running as the user 'pi'. – Charles Pavlov May 19 at 3:28
  • I'm running '$ python3 script.py' outside of the virtual environment so I assume it's running as the user 'pi' and using it's default permissions. As far as I know 'pi' is part of the i2c group. Note that I only encounter issues when I try to run it from inside a vevn. – Charles Pavlov May 19 at 3:41
0

Question

  1. i2cdetect -y 1 returns Error, run using sudo works.

  2. add user name to group i2c hasn't had any visible effect at all.

  3. Allow user to access i2c without using sudo but I have no idea how.

Answer

  1. sudo i2cdetect -y 1 can detect means I2C more or less OK.

  2. Perhaps the user name has not enough permission.

  3. Your program can do sudo things if you run it in desktop GUI IDLE Python,

idle python 353

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.