0

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

Tauseef_Ahmed
  • 343
  • 3
  • 8
  • 18
  • i think you can remove `music_cmd = music_command if music_cmd == 'play music':` from `test.py` since you are already checking `if i['tag'] == "play-music":` – PRMoureu Sep 23 '17 at 10:14
  • Now it is giving another error in main.py at the line test.playmusic(soundfile) NameError: name 'soundfile' is not defined – Tauseef_Ahmed Sep 23 '17 at 10:36
  • how did you pass it in `test.py` to make it work ? you could use the same – PRMoureu Sep 23 '17 at 10:45
  • Please provide a [minimal, complete and verifiable example](https://stackoverflow.com/help/mcve) that we can copy and run. Also, post the complete error messages (stack traces) that you see. – skrx Sep 23 '17 at 11:00
  • @PRMoureu I have edited the question. – Tauseef_Ahmed Sep 23 '17 at 11:23
  • @skrx the entire code in main.py is above 300 lines, so I posted only the part of code which is linked to other file. (`test.py`). What I want to do is simply set a varible in `main.py` and send it to `test.py` which is supposed to process that variable, and return the value back to `main.py` which would play the song. – Tauseef_Ahmed Sep 23 '17 at 11:31
  • @Tauseef_Ahmed my bad, i didn't pay attention to the last lines, a quick fix could be to replace `if music_cmd == 'play music':` by `def main():` in `test.py`, then you import `main` instead of `playmusic` and you just execute `main()` – PRMoureu Sep 23 '17 at 11:44
  • @PRMoureu You are a life saver. Thanks. It got fixed. – Tauseef_Ahmed Sep 23 '17 at 12:00
  • glad to help you ! It will be even better to wrap only the `try/except` part – PRMoureu Sep 23 '17 at 12:29

0 Answers0