I have a python script (main.py)
that sends command to another python script (test.py)
to play music and (test.py)
plays music from the given directory. Both files are working fine separately. But when I pass a variable from main
to test
, it returns errors. (I'm not passing the variable correctly, of course. But I can't figure out where I'm making a mistake).
I get following error everytime:
music_cmd = music_command
NameError: name 'music_command' is not defined
My scripts are:
main.py
import test
from test import playmusic
if i['tag'] == "play-music":
music_command = sentence #sentence is the input command from questions directory.
music_played = test.playmusic(soundfile)
print('playing...')
test.py
import os
import pygame
import random
from glob import glob
music_cmd = music_command
if music_cmd == 'play music':
def playmusic(soundfile):
pygame.init()
pygame.mixer.init()
clock = pygame.time.Clock()
pygame.mixer.music.load(soundfile)
pygame.mixer.music.play()
print("Playing...")
while pygame.mixer.music.get_busy():
clock.tick(1000)
def stopmusic():
"""stop currently playing music"""
pygame.mixer.music.stop()
def getmixerargs():
pygame.mixer.init()
freq, size, chan = pygame.mixer.get_init()
return freq, size, chan
def initMixer():
BUFFER = 3072 # audio buffer size, number of samples since pygame 1.8.
FREQ, SIZE, CHAN = getmixerargs()
pygame.mixer.init(FREQ, SIZE, CHAN, BUFFER)
try:
initMixer()
filenames = glob('./music/*.mp3')
playmusic(random.choice(filenames))
except KeyboardInterrupt: # to stop playing, press "ctrl-c"
stopmusic()
print ("\nPlay Stopped by user")
I want to send the question to test.py and after song is selected, it gets its value and plays it.
I hope I stated everything clearly.
Edit:
In order to make test.py
run separately, I change the line
music_cmd = music_command
to music_cmd = input('Enter Comamnd')
And then if condition checks if music_cmd
has the value play music
I need this value to come from main.py