首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >导入adafruit_dht脚本时出现问题

导入adafruit_dht脚本时出现问题
EN

Stack Overflow用户
提问于 2021-12-23 13:25:00
回答 1查看 1.5K关注 0票数 1

我正在运行一个覆盆子pi4b,并使用温度/湿度传感器。我在pi上有两个用户帐户:一个是pi,另一个是威尔。

当我在用户"pi“下运行下面的代码时,它工作得很好,当我使用"will”运行它时,它会出现以下错误:

代码语言:javascript
复制
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'

我猜用户“将”无法以某种方式正确地访问库adafruit_dht。有人能说明一下如何解决这个问题吗?

代码:

代码语言:javascript
复制
# 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

如果我尝试以"will“的形式运行adafruit pip install,就会得到:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2022-01-29 18:08:56

不是一个完美的解决方案,但我有同样的问题,并能够通过使用以下命令sudo pip3 install Adafruit_DHT --install-option="--force-pi"使其工作

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70462751

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档