0

I tried to write a script that would output the dht 11 sensor data to a .txt file. I reviewed the standard examples given for the Adafruit library.

My code:

import time
import Adafruit_DHT


# infinite while loop

while True:

    sensor = Adafruit_DHT.DHT11
    pin = 4

    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)

    if humidity is not None and temperature is not None:
        print('Temp={0:0.1f}*  Humidity{1:0.1f}%'.format(temperature,humidity))
    else:
        print('Failed to get reading. Try again!')

    time.sleep(5)

When I run it with Thonny IDE, I get the following error message:

Traceback (most recent call last):
  File "/home/pi/Downloads/Sensor1/Adafruit_Python_DHT/examples/simpletest2loop.py", line 5, in <module>
import Adafruit_DHT
ImportError: No module named 'Adafruit_DHT'

I'm able to read the sensor from terminal using the command:

sudo ./AdafruitDHT.py 11 4

with another script listed in: https://tutorials-raspberrypi.com/raspberry-pi-measure-humidity-temperature-dht11-dht22/

I did some research, but I can't figure it out, I kindly ask if someone can help me to troubleshoot this.

Research:

- GPIO Error on Raspberry Pi when following Adafruit Tutorial

pi@raspberrypi:~/Downloads/Sensor1/Adafruit_Python_DHT/examples $ ls -altr total 28

-rw-r--r--  1 pi pi 2035 Jul  5 15:28 simpletest.py
-rwxr-xr-x  1 pi pi 5715 Jul  5 15:28 google_spreadsheet.py
drwxr-xr-x 10 pi pi 4096 Jul  5 15:28 ..
-rwxr-xr-x  1 pi pi 2340 Jul  6 13:19 AdafruitDHT.py
drwxr-xr-x  2 pi pi 4096 Jul 13 14:08 .
-rwxrwxrwx  1 pi pi 1037 Jul 13 14:19 simpletest2loop.py <code>

Seems they are under same user and group. Do i have to copy the library to some folder?

Alex.Han
  • 13
  • 4
  • So the root user can import the module, but you cannot within your IDE. I'd have to guess that you either installed the module only for the root user, or that you and the root user have different Python setups and/or environment variables. – Mark Setchell Jul 13 '18 at 19:31
  • Hi Mark, thanks for the update. I checked seems they are under same user and group – Alex.Han Jul 14 '18 at 10:17
  • AdafruitDHT.py != Adafruit_DHT.py, IMHO – wildplasser Jul 14 '18 at 15:47

1 Answers1

0

If you are having trouble finding Python modules, the best way to sort it out is check:

  • a) where Python is looking, and
  • b) where your modules are.

So, if you want to check where Python is looking you could run this:

python3 -c "import sys; print(','.join(sys.path))"

Sample Output

/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python36.zip
/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6
/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload
/usr/local/lib/python3.6/site-packages
/usr/local/Cellar/numpy/1.14.3_1/libexec/nose/lib/python3.6/site-packages

Then make sure your modules are in one of those places, or add to your PYTHONPATH so that it includes the location where you have installed your modules.


If you are running scripts as root, you should also run the code above as root.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432