How to use a python package with sudo privileges inside Flask?

Peter

I have a Flask setup in my Raspberry Pi 4 Model B via this tutorial.

OS = Ubuntu Server 20.04.2 LTS
Python = 3.8

Please keep in mind that I am using virtual env for my Flask application as shown in the tutorial and my Flask application is running absolutely fine.

Now I installed Adafruit_DHT in the same venv and tried using the following code in one of the endpoints

import Adafruit_DHT
humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, 24)

to which I am getting the following error

File "/usr/local/lib/python3.8/dist-packages/Adafruit_DHT/common.py", line 81, in read
    return platform.read(sensor, pin)
  File "/usr/local/lib/python3.8/dist-packages/Adafruit_DHT/Raspberry_Pi_2.py", line 34, in read
    raise RuntimeError('Error accessing GPIO.')
RuntimeError: Error accessing GPIO.

So, after that, I created a simple python script say z.py and wrote the above code in it. Then, I activated the same Flask venv using

source venv1/bin/activate

And run the script using

python z.py

Again I got the same error. But If I run the above command as sudo

sudo python z.py

then script executed perfectly fine and I got the following response

87.0999984741211 29.399999618530273

So, now the question arrives, how do I use Adafruit_DHT package inside the Flask app with sudo permission?

I don't think setting 777 to www-data group would be the right choice. Or running the Flask app as sudo user would be a great idea.

I have tried installing Adafruit_DHT package globally with sudo, but still I have to execute z.py as sudo

So what is the correct way to do this?

Chris

I believe the package will be trying to access the device /dev/gpiomem (possibly (/dev/gpiochip0 or /dev/gpiochip1).

I think the neatest way to address this would be have those devices be owned by a group other than root and give that group permission to access the device, e.g.

sudo su
groupadd gpio
chgrp gpio /dev/gpio*
chmod g+rw /dev/gpio*

Then I'd go ahead and add your user to that group (by default this is ubuntu, but you may have created another user):

usermod -a -G gpio ubuntu

Now you've created a group called "gpio" that now has permissions to access your Pi's GPIO, and added your user to that group.

Please note, I have not tested this

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to use Vlc with sudo privileges?

How to check if user has sudo privileges inside the bash script?

How do I correctly import and use a classes inside of a package in python

How can run sudo commands inside another user, or grant a user the same privileges as an admin?

How to use sudo inside a docker container?

How to use a python virtualenv with sudo?

How does sudo achieve changing the privileges of a process?

How to install program locally without sudo privileges?

How to execute bash command with sudo privileges in Java?

How to build application without sudo privileges?

How to add user with sudo privileges in Solaris

How to add user with sudo privileges in Solaris

How to launch Brackets from terminal with sudo privileges?

Flask: How to use app context inside blueprints?

How to set correct privileges to use Python Azure SDK for Graph?

How to use Python Generator in flask

How to detect the python package name inside the script

Running a Python CGI Script with Sudo Privileges on the Apache Server

How to get a program running with root privileges without using su or sudo

how come sudo give root privileges for a user not in /etc/sudoers?

How do Ubuntu and Debian manage $HOME for users with sudo privileges?

Always Sudo Privileges

Debugging in pyCharm with sudo privileges?

Restore sudo privileges

Python Flask : Variable inside {% %}

how to access and use sqlalchemy methods inside flask sqlalchemy properly

How to use `FLASK_ENV` inside configuration objects?

How to tell Flask (.flaskenv) which python to use

How to make Flask use Python 3 by default

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive