簡體   English   中英

Python 腳本導入 adafruit_dht 問題

[英]Python script problem importing adafruit_dht

我正在運行覆盆子 pi4b 並使用溫度/濕度傳感器。 我在 pi 上有兩個用戶帳戶:一個是“pi”,另一個是“will”。

當我在用戶“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

如果我嘗試運行adafruit pip install as "will",我會得到:

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

不是一個完美的解決方案,但我遇到了同樣的問題,並且能夠通過使用以下命令使其工作sudo pip3 install Adafruit_DHT --install-option="--force-pi"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM