Python DHT22 传感器导入 Adafruit_DHT 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26246239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DHT22 Sensor import Adafruit_DHT error
提问by FooLingYu2
So I've properly attached DHT22 Humidity Sensor to my BeagleBone Black Rev C. I'm running OS Mavericks on my MacBook Pro and I followed the directions provided by Adafruit on how to use my DHT22
因此,我已将 DHT22 湿度传感器正确连接到我的 BeagleBone Black Rev C。我在 MacBook Pro 上运行 OS Mavericks,并按照 Adafruit 提供的有关如何使用我的 DHT22 的说明进行操作
The website I used was pretty clear: https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/software-install-updated
我使用的网站非常清楚:https: //learn.adafruit.com/dht-humity-sensing-on-raspberry-pi-with-gdocs-logging/software-install-updated
Also here is the github files I cloned: https://github.com/adafruit/Adafruit_Python_DHT
这里也是我克隆的 github 文件:https: //github.com/adafruit/Adafruit_Python_DHT
I put in these lines:
我把这些行:
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT
sudo apt-get upgrade
sudo apt-get install build-essential python-dev
sudo python setup.py install
cd examples
sudo ./AdafruitDHT.py 22 P8_11
I am successful until that last line. Once I enter that last line (sudo ./AdafruitDHT.py 22 P8_11), I get the following error message:
直到最后一行我都成功了。一旦我输入最后一行(sudo ./AdafruitDHT.py 22 P8_11),我就会收到以下错误消息:
Traceback (most recent call last):
File "./AdafruitDHT.py", line 23, in <module>
import Adafruit_DHT
ImportError: No module named Adafruit_DHT
I know there is an Adafruit_DHT file somewhere because when I ls in the Adafruit_Python_DHT directory, I get this:
我知道某处有一个 Adafruit_DHT 文件,因为当我进入 Adafruit_Python_DHT 目录时,我得到了这个:
root@beaglebone:~/Adafruit_Python_DHT# ls
Adafruit_DHT examples ez_setup.py ez_setup.pyc LICENSE README.md setup.py source
I've tried reinstalling the setup.py, but the outcome is still the same.
我试过重新安装 setup.py,但结果还是一样。
I've followed all the directions Adafruit provided, but I just can't seem to get past this. Any idea on what is going on? It seems like a simple problem, but it's proving to be one major obstacle in getting readings from my DHT22. If there is more information needed to help answer this problem please let me know.
我遵循了 Adafruit 提供的所有说明,但我似乎无法克服这一点。知道发生了什么吗?这似乎是一个简单的问题,但事实证明这是从我的 DHT22 获取读数的一个主要障碍。如果需要更多信息来帮助回答这个问题,请告诉我。
回答by PS1
It seems that your script cannot find the "Adafruit_DHT" module. There are two ways.
您的脚本似乎找不到“Adafruit_DHT”模块。有两种方法。
Run the file in the terminal as "Python Adafruit_Python_DHT"
Add the following code at the first line of your script. Should I put #! (shebang) in Python scripts, and what form should it take?
在终端中将文件作为“Python Adafruit_Python_DHT”运行
在脚本的第一行添加以下代码。 我应该把#! (shebang) 在 Python 脚本中,它应该采用什么形式?
回答by PS1
Ok,try running this script with "sudo".
好的,尝试使用“sudo”运行此脚本。
import sys
import Adafruit_DHT
def main():
sensor_args = { '11': Adafruit_DHT.DHT11,
'22': Adafruit_DHT.DHT22,
'2302': Adafruit_DHT.AM2302 }
if len(sys.argv) == 3 and sys.argv[1] in sensor_args:
sensor = sensor_args[sys.argv[1]]
pin = sys.argv[2]
else:
print 'usage: sudo ./Adafruit_DHT.py [11|22|2302] GPIOpin#'
print 'example: sudo ./Adafruit_DHT.py 2302 4 - Read from an AM2302 connected to GPIO #4'
sys.exit(1)
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if humidity is not None and temperature is not None:
print 'Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity)
else:
print 'Failed to get reading. Try again!'
if __name__ == '__main__':
main()
回答by ndb23
Easy fix:
轻松修复:
cd Adafruit_Python_DHT
sudo apt-get update
sudo apt-get install build-essential python-dev python-openssl
sudo python setup.py install
Try to run sudo ./AdafruitDHT.py ## ## ...file again
sudo ./AdafruitDHT.py ## ## ...再次尝试运行文件
You may have forgot to run the setup properly.
您可能忘记正确运行安装程序。

