Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

Python script fails when run by root
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Python script fails when run by root

by paulcrawford on Mon Jun 29, 2020 2:39 pm

I have the following python script that needs to be run from within a c program which is running under root. The script works fine being run by a normal user but with sudo. This the script:

Code: Select all | TOGGLE FULL SIZE
import time
import board
import busio
import digitalio

from adafruit_mcp230xx.mcp23017 import MCP23017
i2c = busio.I2C(board.SCL, board.SDA)
mcp = MCP23017(i2c)
pin2 = mcp.get_pin(2)
pin2.switch_to_output(value=True)
time.sleep(0.5)
pin2.switch_to_output(value=False)


If I run the script from the CLI in the root account, the following error occurs:

Code: Select all | TOGGLE FULL SIZE
root@na:/home/daco# python3 lfploadoff.py
Traceback (most recent call last):
  File "lfploadoff.py", line 2, in <module>
    import board
  File "/home/daco/src/src/adafruit-blinka/src/board.py", line 41, in <module>
    if board_id == ap_board.NODEMCU:
AttributeError: module 'adafruit_platformdetect.board' has no attribute 'NODEMCU'


Can someone please help me determine what changes to make to allow root to run this script?

paulcrawford
 
Posts: 11
Joined: Sun Sep 24, 2017 10:00 am

Re: Python script fails when run by root

by siddacious on Tue Jun 30, 2020 12:40 pm

I'd suggest using sudo to have the root user run the script as whichever user you're having success running it as:

http://researchhubs.com/post/computing/ ... command%20.

More generally I would suggest that you try to avoid running the C program (or anything else) as root unless strictly needed. If the program needs to do something that is restricted to root, you can grant it just those specific privileges.

siddacious
 
Posts: 271
Joined: Fri Apr 21, 2017 3:09 pm

Re: Python script fails when run by root

by paulcrawford on Tue Jun 30, 2020 2:20 pm

Thank you for your response and I appreciate the problems of running C programs under root, but it does not change the fact that I cannot run the python script from the CLI using root. The error I noted already is:

Code: Select all | TOGGLE FULL SIZE
AttributeError: module 'adafruit_platformdetect.board' has no attribute 'NODEMCU'


Do you know what causes this error?

I did try, as you suggested, using sudo -u and got the following (the same error):

Code: Select all | TOGGLE FULL SIZE
root@na:/home/daco# sudo -u daco python3 lfploadoff.py
Traceback (most recent call last):
  File "lfploadoff.py", line 2, in <module>
    import board
  File "/home/daco/src/src/adafruit-blinka/src/board.py", line 41, in <module>
    if board_id == ap_board.NODEMCU:
AttributeError: module 'adafruit_platformdetect.board' has no attribute 'NODEMCU'


How can this AttributeError be fixed?

Using the user daco from the CLI gives the following:

Code: Select all | TOGGLE FULL SIZE
daco@na:~$ python3 lfploadoff.py
Traceback (most recent call last):
  File "lfploadoff.py", line 2, in <module>
    import board
  File "/home/daco/src/src/adafruit-blinka/src/board.py", line 93, in <module>
    from adafruit_blinka.board.odroidn2 import *
  File "/home/daco/src/src/adafruit-blinka/src/adafruit_blinka/board/odroidn2.py", line 3, in <module>
    from adafruit_blinka.microcontroller.amlogic.s922x import pin
  File "/home/daco/src/src/adafruit-blinka/src/adafruit_blinka/microcontroller/amlogic/s922x/pin.py", line 3, in <module>
    GPIO496 = Pin((0, 0))
  File "/home/daco/src/src/adafruit-blinka/src/adafruit_blinka/microcontroller/generic_linux/libgpiod_pin.py", line 25, in __init__
    self._chip = gpiod.Chip(str(pin_id[0]), gpiod.Chip.OPEN_BY_NUMBER)
PermissionError: [Errno 13] Permission denied


If I use sudo on the above command everything works just fine.

The permission error above seems to be in the module gpiod but I do not know where it is located to see its permissions. I tried:

Code: Select all | TOGGLE FULL SIZE
sudo find / -name gpiod


but it yielded nothing. It seems that if gpiod permissions could be changed then the user daco could be used to run the script without sudo.

paulcrawford
 
Posts: 11
Joined: Sun Sep 24, 2017 10:00 am

Re: Python script fails when run by root

by siddacious on Tue Jun 30, 2020 2:38 pm

As with just about everything in *nix/POSIX land it's probably an environment issue (or permissions issue which you've pointed out). I believe that by default sudo only affects permissions/uid and you have to specify another flag to use the specified user's environment as well. I'm not familiar with the internals of platform_detect, but one or more parts of that puzzle must be getting different inputs to give you different results.

siddacious
 
Posts: 271
Joined: Fri Apr 21, 2017 3:09 pm

Re: Python script fails when run by root

by siddacious on Tue Jun 30, 2020 11:34 pm

I apologize; I re-read my previous response and I pretty much glossed over the fact that you had already come to the same conclusion and had a candidate in mind. I'll dig a bit deeper to see if I can be more useful, otherwise I'll find someone who can.

siddacious
 
Posts: 271
Joined: Fri Apr 21, 2017 3:09 pm

Re: Python script fails when run by root

by MakerMelissa on Wed Jul 01, 2020 1:53 pm

Hi paulcrawford,

Which board are you trying to run Blinka on?

Certain boards that make use of libgpiod require the script to be run as root due to libgpiod needing root access.

Melissa

MakerMelissa
 
Posts: 146
Joined: Wed Jun 05, 2013 2:10 am

Re: Python script fails when run by root

by MakerMelissa on Wed Jul 01, 2020 1:57 pm

Oh, I just saw in the error you're using an Odroid N2. Yeah, that needs to be run as root. It also appears I misread the message.

Try running this:
Code: Select all | TOGGLE FULL SIZE
pip3 uninstall adafruit-blinka
sudo pip3 install --upgrade --force-reinstall adafruit-blinka

That should remove it from being a user install and force it to be a system install.

MakerMelissa
 
Posts: 146
Joined: Wed Jun 05, 2013 2:10 am

Re: Python script fails when run by root

by paulcrawford on Thu Jul 02, 2020 9:06 am

Thank you so much for your help. I executed the commands you provided and now root can run the script without errors.

Just for the record though the first command did not work but the second did:

Code: Select all | TOGGLE FULL SIZE
daco@na:~/src/src/adafruit-blinka$ pip3 uninstall adafruit-blinka
Can't uninstall 'Adafruit-Blinka'. No files were found to uninstall.
daco@na:~/src/src/adafruit-blinka$ sudo pip3 install --upgrade --force-reinstall adafruit-blinka
[sudo] password for daco:
The directory '/home/daco/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/daco/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting adafruit-blinka
  Downloading https://files.pythonhosted.org/packages/c4/04/a1722e7d796f4226ded462395c849d73d2133fc19196cd862a41bd09ab11/Adafruit-Blinka-5.2.0.tar.gz (106kB)
    100% |████████████████████████████████| 112kB 1.7MB/s
Collecting Adafruit-PlatformDetect>=2.11.1 (from adafruit-blinka)
  Downloading https://files.pythonhosted.org/packages/4f/a7/c1ef0f336c4d302e1d1888db30eb46f393515fe36696d48e89d8ff2b484d/Adafruit-PlatformDetect-2.14.0.tar.gz
Collecting Adafruit-PureIO>=1.1.5 (from adafruit-blinka)
  Downloading https://files.pythonhosted.org/packages/01/ee/da6d2c6848571a65eaa0f1e37b1cd13ba8fefdea962455faabef7c61e7f4/Adafruit_PureIO-1.1.5.tar.gz
Collecting pyftdi>=0.40.0 (from adafruit-blinka)
  Downloading https://files.pythonhosted.org/packages/80/9e/0893cd66e60946b881651012bbbb673a6bac1ceddf71d196fa17fb09dffa/pyftdi-0.51.2-py3-none-any.whl (150kB)
    100% |████████████████████████████████| 153kB 2.4MB/s
Collecting sysv_ipc (from adafruit-blinka)
  Downloading https://files.pythonhosted.org/packages/57/8a/9bbb064566320cd66c6e32c35db76d43932d7b94348f0c4c1e74d03ec261/sysv_ipc-1.0.1.tar.gz (102kB)
    100% |████████████████████████████████| 102kB 5.2MB/s
Collecting pyusb>=1.0.0 (from pyftdi>=0.40.0->adafruit-blinka)
  Downloading https://files.pythonhosted.org/packages/5f/34/2095e821c01225377dda4ebdbd53d8316d6abb243c9bee43d3888fa91dd6/pyusb-1.0.2.tar.gz (54kB)
    100% |████████████████████████████████| 61kB 4.1MB/s
Collecting pyserial>=3.0 (from pyftdi>=0.40.0->adafruit-blinka)
  Downloading https://files.pythonhosted.org/packages/0d/e4/2a744dd9e3be04a0c0907414e2a01a7c88bb3915cbe3c8cc06e209f59c30/pyserial-3.4-py2.py3-none-any.whl (193kB)
    100% |████████████████████████████████| 194kB 1.6MB/s
Installing collected packages: Adafruit-PlatformDetect, Adafruit-PureIO, pyusb, pyserial, pyftdi, sysv-ipc, adafruit-blinka
  Found existing installation: Adafruit-PlatformDetect 1.3.0
    Uninstalling Adafruit-PlatformDetect-1.3.0:
      Successfully uninstalled Adafruit-PlatformDetect-1.3.0
  Running setup.py install for Adafruit-PlatformDetect ... done
  Found existing installation: Adafruit-PureIO 0.2.3
    Uninstalling Adafruit-PureIO-0.2.3:
      Successfully uninstalled Adafruit-PureIO-0.2.3
  Running setup.py install for Adafruit-PureIO ... done
  Running setup.py install for pyusb ... done
  Found existing installation: sysv-ipc 1.0.0
    Uninstalling sysv-ipc-1.0.0:
      Successfully uninstalled sysv-ipc-1.0.0
  Running setup.py install for sysv-ipc ... done
  Found existing installation: Adafruit-Blinka 2.3.0
    Can't uninstall 'Adafruit-Blinka'. No files were found to uninstall.
  Running setup.py install for adafruit-blinka ... done
Successfully installed Adafruit-PlatformDetect-2.14.0 Adafruit-PureIO-1.1.5 adafruit-blinka-5.2.0 pyftdi-0.51.2 pyserial-3.4 pyusb-1.0.2 sysv-ipc-1.0.1


Thanks again.

paulcrawford
 
Posts: 11
Joined: Sun Sep 24, 2017 10:00 am

Please be positive and constructive with your questions and comments.