问题
I have some trouble to execute a Python file from a Codeigniter controller's function.
~/application/controllers/Lights.php :
header("Access-Control-Allow-Origin: *");
class Lights extends CI_Controller {
public function __construct() {
parent::__construct ();
$this->load->helper ( array ('url', 'form') );
}
public function turnOn() {
system('sudo python test.py > /dev/null 2>/dev/null &');
}
}
but when I try to execute a system call, like :
system("sudo gpio mode 15 out");
this works perfectly.
回答1:
Provide the full path to python
binary and script
, i.e.:
system('/full/path/to/python /full/path/to/test.py > /dev/null 2>/dev/null &');
If the python
binary is already on the PATH
of your apache user (apache
/www-user
), you may ignore the full path.
回答2:
yes I has a problem like that and Thank you for Pedro Lobito answer. There need a full path to call python file from CI from the root folder.
such as
system('sudo -u root -S python /var/www/4led/turnon1.py');
*sudo -u root -S is a command for using root permission with no root password required :D
来源:https://stackoverflow.com/questions/37093408/executing-a-python-file-from-codeigniter