pHATBEAT Python script works in IDLE but not from command line

I’m wanting to make a standalone device that plays a .wav file when a button is pressed, using a pHATBEAT and a Pi Zero W. The simple Python 3 script I’ve written so far works fine when executed in IDLE, but if I try to run it from the command line I get silence, and a couple of random LEDs light on the pHAT. If I remove the button handlers and just run the bit that plays the sound file, it’s fine. I’m new to all this so it’s hopefully something simple!

import pygame
import time
import phatbeat
import os
whistle = pygame.mixer.Sound("sounds/whistle.wav")
def playpause(pin):
def onoff(pin):
     os.system("sudo shutdown -h now")

Any ideas please?

What command are you using to run it, and what do you see if you run the pwd and ls commands?

I run it using
from the directory containing the script; pwd returns
and ls returns
sounds is the directory containing the .wav file(s).

I have just tried typing the commands in directly on the command line. All is OK until I get to

whistle = pygame.mixer.Sound(“sounds/whistle.wav”)

This returns

Traceback (most recent call last):
   File <"stdin">, line 1, in module

I guess that is the problem, but how to solve it? The .wav file is 1.0MB. Thanks.

Is the “sounds/whistle.wav” directory in your current working directory?

“MemoryError” seems like it might be a red herring- I can’t see a 1.0MB wav file being an issue, unless it’s accidentally loaded hundreds of times or some other weirdness.

Could it be a $path problem? You can try to spell out the full path to the wav file.

Try changing
whistle = pygame.mixer.Sound(“sounds/whistle.wav”)

whistle = pygame.mixer.Sound(“/home/pi/GPIOMB/sounds/whistle.wav”)

Thanks both. I’ve tried mov the .wav file to the same directory as the .py file and also specifying the full path name. Both result in silence and a return to the command prompt when run from the command line, and the same MemoryError if I type the commands in separately. I’m assuming the pygame library is well proven and unlikely to be the issue, is that reasonable? Is there a log file somewhere that might offer a clue? I’ve a couple of RPi books on order which I’m hoping will help clarify how the Pi works.

I’m more used to programming microcontrollers in assembly language when it’s much easier to see exactly what’s going on, and it’s 30 years or more since I used Unix!

This works fine from the command line:

import pygame
import time
import phatbeat
import os
whistle = pygame.mixer.Sound(“sounds/whistle.wav”)
#def playpause(pin):

So it would appear to be something to do with the (commented out) phatbeat code?

I’ve abandoned the phatbeat module and am now using gpiozero instead. It’s working OK :)