1

I have a Python3 script (readtemp.py) to read temperature and humidity of a DHT22. I'm using adafruit_dht module. it executes perfectly from terminal command line with:

    python3 readtemp.py

or

    ./readtemp.py

At the head of the Python script I have:

    #!/usr/bin/python3
    import time
    import board
    import adafruit_dht

I put a line in Apache PHP:

$output = exec("python3 ./readtemp.py");

or

$output = exec("./readtemp.py");

or

$output = shell_exec("python3 ./readtemp.py");

or

$output = shell_exec("./readtemp.py");

and all work but when the page loads the Python script is executed but the import gets an error. In apache error.log:

Traceback (most recent call last):
  File "./readtemp.py", line 4, in <module>
    import adafruit_dht
ModuleNotFoundError: No module named 'adafruit_dht'

It looks like apache isn't letting python load an external module. Any ideas to get apache to allow python access the exeternal modules?

5
  • Have you installed the module adafruit_dht using pip3?
    – JD12
    Jan 7 at 15:11
  • Yes, it was installed with pip3
    – SailorBoy
    Jan 7 at 16:07
  • Did you use sudo pip3 ... so that the module is available for all users?
    – Dougie
    Jan 7 at 16:43
  • yes, sudo was used to install.
    – SailorBoy
    Jan 7 at 17:19
  • Just to check, I sudo pip3 uninstall... then then installed again with sudo pip3 install... Now apache PHP page hangs on the exec() and never goes on to the rest of the PHP and nothing shows in the apache error.log.
    – SailorBoy
    Jan 7 at 17:47
1

If you search this Forum you will find MANY questions about DHT22.

The Adafruit code is notoriously unreliable and CAN NOT be run from PHP because it requires root access.

See Reliable temperature/humidity logging with Python and a DHT11 for the code I use.

2
  • Thanks. I'll give it a try.
    – SailorBoy
    Jan 8 at 1:00
  • Thanks again. Works great and my application is running on the webserver.
    – SailorBoy
    Jan 8 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.