Running python code from php

I have a python script that php needs to run:

     $command = escapeshellcmd('/home/Desktop/');
     $output = shell_exec($command);
     echo $output;



The python script output is binary, but I am getting the following error in the log: Can't access X Display, is $ DISPLAY set?

The PHP code works fine from the terminal, but no luck when I try to run it from the browser. Any idea what's going on? Ideally, I don't want to change my program. I want to know how you can fix the X Display error. Is there a way to check if $ DISPLAY is set correctly? (I am working with Ubuntu)

I tried this: pidof X && echo "yup X server is running" on my terminal and it says yup x server is running!


source to share

3 answers

Add the following text to the first line of your Python script:



Without this, the kernel does not know which interpreter is running your script from, and can try to run it with /usr/bin/import

(because that word probably appears on the first line of the script). The utility import

requires access to the X11 screen (it is basically a screenshot utility) so you get this error.



For a python file, you may need to open a window to run. You say you saw it running in the terminal? What is Is this propitiation? If you try to use this as a command in your PHP: (and not 100% that shell escaping will not remove this, so it may need to be commented out)

python -c \'print \"Test\"\' 


and see if you return the output text. If it is a python problem, not PHP, and the file can instantiate what is needed to set $ DISPLAY var. PHP does not set $ DISPLAY var as these are shell commands, not GUI commands.



try it popen

$command = "/usr/bin/python /home/Desktop/";
$handle = popen($command, "r");


"r" for reading

$read = fread($handle, 10);


10 is the size of the output you want to take,

echo $read ;


hope this helps,



All Articles