Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a python code with php

Tags:

python

php

I have a python script which is needed to be run by php:

     <?php
     $command = escapeshellcmd('/home/Desktop/test.py');
     $output = shell_exec($command);
     echo $output;

     ?>

The out put of the python script is a binary file but I get the following error in the log: Unable to access the X Display, is $DISPLAY set properly?

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

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

like image 429
Jack Avatar asked Jul 30 '26 16:07

Jack


1 Answers

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

#!/usr/bin/python

Without this, the kernel doesn't know what interpreter to run your script with, and may end up trying to launch it using /usr/bin/import (because that word probably appears on the first line of the script). The import utility requires access to the X11 display (it's a screenshot utility, basically), which is why you're getting this error.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!