简体   繁体   中英

import RPi.GPIO as GPIO not working in python when called from php

i have the same problem as this question, python import fails when called from PHP but now for my another import.

The script is working from the command line ( python uit.py ) and a test.py whic says 'hello' also works, but when the uit.py (source below) is called from php, it doesn't work. All i get is return code 1, i tried exec(), passthru() and system(), and none of them return anything other than '1' I'm quite new to linux and python, so i don't know how to set the path to the import direct instead of relative (if that should help)

#!/usr/bin/python
# Hello world python
import RPi.GPIO as GPIO
print "Start";

GPIO.setmode(GPIO.BOARD);
GPIO.setup(16, GPIO.OUT)
GPIO.output(16, True)

print "Licht uit";

EDIT: What i noticed now, is that when the script from the command line, i have to sudo it because the script says it wants to run as root (i get an error when i don't run it as root saying 'maybe run as root?'.

Can that be the reason for not working? That the python script gets called from PHP as the user PHP is running under?

I did executing the statement with sudo (see the comments) but i've also read that maybe the php user is not allowed to sudo ?

What i noticed now, is that when the script from the command line, i have to sudo it because the script says it wants to run as root (i get an error when i don't run it as root saying 'maybe run as root?'.

Can that be the reason for not working? That the python script gets called from PHP as the user PHP is running under?

Yes, that's your problem. The web server runs as www-data , not as root. (This user doesn't even have sudo access by default.) If you check the web server error log, you will probably see the "maybe run as root?" error message in there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM