0

recently I'm doing a small project, and a part of it involves running a python script using PHP. to explain further:

Python:

from playsound import playsound

playsound("path/of/the/audio")
print("playsound executed")

PHP:

echo system('python3 pythonapp.py');

I used a print function to make sure my playsound func executed properly which it did, I even double checked my python file by running it directly in the terminal which plays the audio without any problem, what I would like to know, is why when I used PHP to call the python script it wont play the audio. but using phpmyadmin, the output "playsound executed" is shown.

Thank you for your time! hope you can help me resolve this.

Edit: Ubuntu is running on a Raspberry Pi 4B and I'm using phpmyadmin/apache2 to run my php script

5
  • it is unclear how you are using PHP ... is it used in a web server? ... where do you expect the sound to be generated? ... what does Raspberry PI have to do with the question?
    – jsotola
    Commented Sep 6, 2021 at 16:49
  • ah i see, will edit my post sorry for this.
    – Paul O.
    Commented Sep 15, 2021 at 2:41
  • I installed ubuntu on raspberry pi 4b, since this projects originally implemented on a windows 10 OS, I was wondering if someone encountered this kind of problem. btw I want to play the sound on the device itself.
    – Paul O.
    Commented Sep 15, 2021 at 2:45
  • does the python script work by itself? You don't need python at all, call paplay path/of/the/audio. Also are you running in Ubuntu server or desktop?
    – rtaft
    Commented Sep 15, 2021 at 17:05
  • try raspberry pi os if the issue is with generating a sound at all. If issue is the php-python call, try to debug the environment of the user running php.
    – Abel
    Commented Sep 15, 2021 at 19:43

1 Answer 1

1

One likely cause is missing permissions: your script works when you run it as the pi user, but apache runs it as www-data.

Try sudo -u www-data python3 pythonapp.py and see if it still works. If not, you'll need to give permissions to all users to access your script and the sound file.

You will also need to add www-data user to audio group.

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.