Sound not playing in python via cmd but works through idle - python

I tried different ways of playing sound through cmd or any other terminal like that of vscode but python using any module (pygame,winsound,playsound,pyglet,etc.) doesn't play sound. I tried and found that idle plays all the sounds. Is there any way by which even other terminals and ides can play sound?
If you need more info please inform
I even tried using ctrl+g in cmd. it produced sound but when I did the save by using .bat file.It doesn't produce any Sound
Edit:-I work in windows and none other than any terminal like powershell works.It works only through Idle
Edit2:-I used many different codes but the basic code used was
import winsound
# for playing note.mp3 file
winsound.PlaySound('click2.wav',winsound.SND_ASYNC)
print('playing sound using playsound')

Related

Beginner question about finding Python script in IDLE shell

I'm a beginner in Python with no prior programming experience, so bear with me here.
I installed Python, started playing with it (typing variables, playing with math operations) in the Shell window and everything is fine. I open a New Window and started writing a simple script. Something like this:
print (1+1)
I press Run Module, and I am asked to name and save the script first. I do so by calling it firstscript.py, and save it to my desktop.
Now I press Run Module, and in the shell window 2 is printed on the screen. Everything is fine. I close Python, and open it up again. Now in the shell window, I type firstscript.py and I get the red message NameError: name 'firstscript' is not defined.
I can run my program only if I open the script file on my desktop and press Run Module from there, but if I try to start it up directly in IDLE Shell by typing its name, I get the error message.
What did I do wrong? Thank you.
Good to see that you are starting with python.
Firstly, you can run the file directly using 'Run Module' only when you have the file open. Once you save the file and quit, you are out of the file editor and back to the terminal.
Simply typing in firstscripty.py will not work as it does not recognize the command.
To run the file from the terminal, use the below code:
python [locationOfFile\]firstscript.py
You can check out this detailed explanation: https://realpython.com/python-idle/#how-to-work-with-python-files
The problem here is the Shell doesnt know that your firstscript.py is sitting on the desktop
The simplest way i suggest using cmd with:
python C:\Users\{your user}\Desktop\firstscript.py

Run python script from c# in unity game?

I'm trying to create the nim game in unity with some nice 3d graphics. For those who don't know, nim is a solved game which means that, assuming both players play perfectly, we can know who will win at the first turn.
I want to make 4 game modes:
Two players who play against each other.
The player plays against the computer who plays perfectly.
The computer plays against a bot the user wrote in python.
The player plays against a bot he wrote in python.
I learnt about this game in computer science class in high school, and we were given an exercise to write a program that will win a simpler version of this game.
That's why I want to include the last two modes: To let students write their bots in python and test it in my game. I think it can turn out very cool.
However, that means I need to figure out how to run a python script from c# script in unity. I know I can use Process in c# to run the external python script, but that requires me to know the location of the python executable.
I could make a settings menu, so the user can set the path to python there, but I don't like the fact that the user will have to deal with path settings.
I learnt that, on windows, I can download an embeddable zip which contains the python interpreter (python.exe) and simply ship this with the game so the user doesn't even need to have python installed on their machine. However, I couldn't find any similar zip for other platforms, specifically linux and mac.
So, my question is how should I run python scripts in unity? Is there a way to embed the python interpreter for both windows, mac and linux, or should I make a settings menu for configuring the python path and use processes?
If somebody can give me an idea for how to use python in unity, I'd really appreciate this.
Note: I want to use python 3 for the scripting, so solutions that work for python 2 only can't help me at all.
Thanks in advance.
You can use System.Diagnostics.Process.Start and start cmd with it
string strCmdText;
strCmdText= "python script.py";
System.Diagnostics.Process.Start("CMD.exe",strCmdText);```
Only python 2.7.5, python 3 is not functional
Python and Unity
but if you just want to use a python as Bot, i suggest you to communicate via TCP (socket) or others ways...

How to display pygame output on cloud9-vnc?

So I'm playing with python3 on Cloud9, got interested in trying pygame and found a recommendation to install cloud9-vnc for a desktop display. Got both of those things to work, but not in tandem. I'm a rather newbish with Linux and VNC, so I'm stuck at the moment. Any chance I can get pygame output on a VNC desktop?
What I have so far is that I've installed pygame using this and cloud9-vnc using this. Pygame import and commands run smoothly (both in terminal and script) and when I run the script with c9vnc I get the link to a VNC desktop. However, the desktop is clear, apart from Ubuntu logo.
The program doesn't actually seems to be running, considering that it doesn't display the text to be printed
In fact, it seems that it's not even started to run.
However, inside the VNC desktop I have the access to complete cloud9 workspace, including installed pygame, which does work, albeit a bit more clunky, compared to cloud9's interface.
So what I want to ask is is there a way for me to write and run a code on cloud9's default interface that would directly display the output in VNC's desktop, with little to no additional interaction?

When running a pygame script as root, no sound is output?

I have written a very simple script for my raspberry pi that loads an uncompressed WAV and plays it - however when I run the script as root (to be able to use GPIO and ServoBlaster), there is no sound output.
I have set the default audio device to a USB sound card, and this works - I have tested this using aplay fx.wav.
Running the pygame script without sudo, the sound plays fine.
What is going on here?
The issue was the sudo command changing the directory in which the script was being run - so running python with sudo -s or simply using an absolute path for the sound fixed it.

pygame.mixer sound not playing when script run from command line

I'm working on a Raspberry Pi project and I have a python script that accepts some serial input and plays sounds depending on the input. I have the script set up and it works just fine when I run it from within the GUI (i.e. startx). If I log out of the GUI and try to run the script from the command line the script executes just fine but my sounds don't play. I just get a momentary static click. I can tell the script is running because I have it printing debug code and the print's work just fine. Is there a way to get the sounds to work from the command line?
I want this script to execute when the Raspberry Pi is turned on without user input which I believe means it will be running from the command line. If there is some reason the sounds simply won't play until the GUI starts up how would I set it up to load the GUI and then execute the script on startup without any user input?
This will be embedded in a prop and will play sounds when some buttons (connected through arduino i.e. serial input) are pressed. So I need a solution that will have it from power on automatically run the script and be able to play the sounds with no keyboard, mouse, or monitor attached.
Turns out it was file path naming. If I have the command line test to the root directory it doesn't work but if I "cd Desktop/containingFolder" then the sounds play. I'll play with how I have the files set up in the python script so it will work.
Updating the path names fixed the issue. I just needed them to be full paths instead of relative ones.

Categories

Resources