1

I am running a raspberry pi4b and making use of a temp/humidity sensor. I have two user accounts on the pi: one is "pi", the other is "will".

When I run the below code under user "pi", it works fine, when I run it using "will", it errors out with:

Traceback (most recent call last):
  File "/tmp/pycharm_project_911/hum_temp.py", line 10, in <module>
    import adafruit_dht
ModuleNotFoundError: No module named 'adafruit_dht'

I am guessing the user "will" cannot somehow access the library adafruit_dht properly. Can anyone shed some light on how to fix this?

Code:

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT


# datetime object containing current date and time
import datetime
import time
import board
import RPi.GPIO as GPIO
import adafruit_dht

# Initial the dht device, with data pin connected to:
dhtDevice = adafruit_dht.DHT22(board.D9)

# you can pass DHT22 use_pulseio=False if you wouldn't like to use pulseio.
# This may be necessary on a Linux single board computer like the Raspberry Pi,
# but it will not work in CircuitPython.
# dhtDevice = adafruit_dht.DHT22(board.D18, use_pulseio=False)

while True:
    try:
        # Print the values to the serial port
        temperature_c = dhtDevice.temperature
        # datetime object containing current date and time
        now = datetime.datetime.now()
        #temperature_f = temperature_c * (9 / 5) + 32
        humidity = dhtDevice.humidity
        print(now.strftime("%Y-%m-%d %H:%M:%S"))
        print(
                (" Temp: {:.1f} C    Humidity: {}% ".format(
                temperature_c, humidity)
            )
        )

    except RuntimeError as error:
        # Errors happen fairly often, DHT's are hard to read, just keep going
        print(error.args[0])
        time.sleep(2.0)
        continue
    except Exception as error:
        dhtDevice.exit()
        raise error
    #except KeyboardInterrupt:
    # here you put any code you want to run before the program
    # exits when you press CTRL+C
    # print "\n", counter # print value of counter
    time.sleep(5.0)
    #finally:
    GPIO.cleanup() # this ensures a clean exit

If I try running adafruit pip install as "will", I get:

Collecting Adafruit_DHT
  Using cached Adafruit_DHT-1.4.0.tar.gz (15 kB)
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: Adafruit-DHT
  Building wheel for Adafruit-DHT (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: /usr/bin/python3 -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-7ly2kmxi/adafruit-dht_34d224c3c67f4069a502b69fbcc56826/setup.py'"'"'; __file__='"'"'/tmp/pip-install-7ly2kmxi/adafruit-dht_34d224c3c67f4069a502b69fbcc56826/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-v6u5rkri
       cwd: /tmp/pip-install-7ly2kmxi/adafruit-dht_34d224c3c67f4069a502b69fbcc56826/
  Complete output (1 lines):
  Could not detect if running on the Raspberry Pi or Beaglebone Black.  If this failure is unexpected, you can run again with --force-pi or --force-bbb parameter to force using the Raspberry Pi or Beaglebone Black respectively.
  ----------------------------------------
  ERROR: Failed building wheel for Adafruit-DHT
5
  • 2
    Have you tried running pip install Adafruit_DHT with the user named "will"? Commented Dec 23, 2021 at 13:26
  • i've updtaed the post with the output of that
    – Will Mowat
    Commented Dec 23, 2021 at 13:37
  • As the new error points out, can you run it again with the extra argument, like so: pip install Adafruit_DHT --force-pi Commented Dec 23, 2021 at 13:39
  • 2
    Tried that and it comes back with no such option: --force-pi
    – Will Mowat
    Commented Dec 23, 2021 at 13:45
  • This is purely to with the user account i think, pi can do something will cannot
    – Will Mowat
    Commented Dec 23, 2021 at 13:46

2 Answers 2

5

Not a perfect solution but I had the same issue and was able to get it working by using the following command sudo pip3 install Adafruit_DHT --install-option="--force-pi"

0

Install it like this...

sudo pip3 install Adafruit_Python_DHT

... but still import it like this...

import Adafruit_DHT

Your Answer

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

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