简体   繁体   中英

Python script problem importing adafruit_dht

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

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM