我正在运行一个覆盆子pi4b,并使用温度/湿度传感器。我在pi上有两个用户帐户:一个是pi,另一个是威尔。
当我在用户"pi“下运行下面的代码时,它工作得很好,当我使用"will”运行它时,它会出现以下错误:
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。有人能说明一下如何解决这个问题吗?
代码:
# 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,就会得到:
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发布于 2022-01-29 18:08:56
不是一个完美的解决方案,但我有同样的问题,并能够通过使用以下命令sudo pip3 install Adafruit_DHT --install-option="--force-pi"使其工作
https://stackoverflow.com/questions/70462751
复制相似问题