sudo python3 genera errori ma python3 no

Aug 16 2020

Non capisco perché "sudo python3 temphumlogger2.py" innesca un errore del modulo, mentre "python3 temphumlogger2.py" non esegue alcun problema. Output di seguito e codice di seguito. Voglio che il codice venga eseguito automaticamente all'avvio e devo usare sudo per farlo. Ho provato a reinstallare i vari pacchetti ma non riesco a capire perché sudo causa errori e senza?

pi@raspberrypi:~ $ sudo python3 temphumlogger2.py  
Traceback (most recent call last):  
  File "temphumlogger2.py", line 3, in <module>  
    import adafruit_dht
ModuleNotFoundError: No module named 'adafruit_dht'
pi@raspberrypi:~ $ python3 temphumlogger2.py  
waiting 15 mins  
Temp: 73.9 F / 23.3 C    Humidity: 48.0% 
import time
import board
import adafruit_dht
from datetime import datetime
 
# Initial the dht device, with data pin connected to:
dhtDevice = adafruit_dht.DHT22(board.D4)
 
while True:
    try:
        # Print the values to the serial port
        temperature_c = dhtDevice.temperature
        temperature_f = temperature_c * (9 / 5) + 32
        humidity = dhtDevice.humidity
        with open('/home/pi/thdataupstairs.csv', mode='a') as file_:
            file_.write("{},{},{}".format(datetime.now(),temperature_c, humidity))
            file_.write("\n")
        print("waiting 15 mins")
        print(
            "Temp: {:.1f} F / {:.1f} C    Humidity: {}% ".format(
                temperature_f, temperature_c, humidity
            )
        )
        time.sleep(10)
 
    except RuntimeError as error:
        # Errors happen fairly often, DHT's are hard to read, just keep going
        print(error.args[0])
 
    time.sleep(2.5)

Risposte

1 mechoption Aug 16 2020 at 22:46

Ho usato sudo bashper inserire comandi come sudo e ho trovato lo python3 temphumlogger2.pystesso errore. All'interno di questa finestra di sudo bash ho seguito le istruzioni di CircuitPython qui per installare nuovamente tutti i moduli. Dopodiché temphumlogger.py ora funziona con e senza sudoquando accedo come pi.

Come da commenti penso di aver installato le cose come utente invece di sudo.