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



At the head of the Python script I have:

    import time
    import board
    import adafruit_dht

I put a line in Apache PHP:

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


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


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


$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?

  • Have you installed the module adafruit_dht using pip3?
    – JD12
    Commented Jan 7, 2021 at 15:11
  • Yes, it was installed with pip3
    – SailorBoy
    Commented Jan 7, 2021 at 16:07
  • Did you use sudo pip3 ... so that the module is available for all users?
    – Dougie
    Commented Jan 7, 2021 at 16:43
  • yes, sudo was used to install.
    – SailorBoy
    Commented Jan 7, 2021 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
    Commented Jan 7, 2021 at 17:47

1 Answer 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.

  • Thanks again. Works great and my application is running on the webserver.
    – SailorBoy
    Commented Jan 8, 2021 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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