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:
        # 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))
        print("waiting 15 mins")
            "Temp: {:.1f} F / {:.1f} C    Humidity: {}% ".format(
                temperature_f, temperature_c, humidity
    except RuntimeError as error:
        # Errors happen fairly often, DHT's are hard to read, just keep going
  • 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. Aug 16, 2020 at 2: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, 2020 at 2: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). 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, 2020 at 4: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, 2020 at 9:10

1 Answer 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.

  • 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, 2020 at 22:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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