1

I don't understand why "sudo python3 temphumlogger2.py" triggers a module error, while "python3 temphumlogger2.py" executes no problem. Outputs below, and code below that. I want the code to execute automatically on startup, and need to use sudo to do that. I've tried reinstalling the various packages but can't figure out why sudo causes error and without does not?

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)

Jake
  • 89
  • 10
mechoption
  • 21
  • 6
  • 2
    sudo starts up a new shell with a (possibly) very different environment. Your modules likely don't appear to be installed there because of PATH or something. Log in as root or "sudo bash" or whatever, and just work to get the script working under root. At that point, sudo will likely work as well. – Frank Merrow Aug 16 '20 at 02:25
  • Success! I ran "sudo bash" (hadn't heard of that before), then I went through the CiruitPython install instructions, and now it works. Must have been a PATH issues I guess. I don't know how to tag your comment as an answer...? – mechoption Aug 16 '20 at 02:49
  • 1
    The Adafruit code is unreliable, has problems with permissions and using sudo with python is always a problem (because it is interpreted - Linux prevents permission escalation). https://raspberrypi.stackexchange.com/a/105549/8697 is a much simpler, more reliable solution which doesn't require root privileges. NOTE sudo bash is a serious security risk. – Milliways Aug 16 '20 at 04:32
  • Please create an answer about your solution and accept it after two days. There is nothing wrong by self answering. Only accepting an answer will finish the question and it will not pop up again and again for years. – Ingo Aug 16 '20 at 09:10

1 Answers1

1

I used sudo bash to enter commands as sudo and found python3 temphumlogger2.py had same error. Within this sudo bash window I followed the CircuitPython instructions here to install all the modules again. After that temphumlogger.py now runs with and without sudo when I log in as pi.

As per comments I think I installed things as user instead of sudo.

mechoption
  • 21
  • 6
  • When you install the python stuff from Adafuit use sudo python3 setup.py install that will install it for python3 and ALL users. If you don't install it with sudo then it's only available for the user that installed it (unless you muck about with sys.path.append(...) to add the private library to the path in your python3 program. – Dougie Aug 16 '20 at 22:45