Why am I unable to load an image using pygame module? - python

Following an online game building tutorial on Youtube, I am trying to build a game but whenever i enter this code it always comes out with this syntax error:
line 11, in <module>
icon = pygame.image.load('burger.png')
pygame.error: Couldn't open burger.png
what should I do?

Probably nothing to do with Pygame itself. But your file location. Do you have it in the same folder as your .py script (aka your root folder)? If yes, check this thread out: Opening images with Python

Related

making an executable with pyinstall: trouble with vlc module (linux)

I want to make an executable out of a gui youtube audio player I made.
There is a problem with the vlc module. When I just make the executable (on Linux), I can't open the file.
Opening in the terminal gives the following error:
Traceback (most recent call last):
File "ytplayer.py", line 39, in
player = instance.media_player_new()
AttributeError: 'NoneType' object has no attribute 'media_player_new'
[24084] Failed to execute script 'ytplayer' due to unhandled exception!
in my code I have:
from vlc import Instance
and
vlc_instance = Instance()
player = vlc_instance.media_player_new()
I found some information on the net about this problem, but no solution yet. This is one post on the internet:
By specifying a VLC_PLUGIN_PATH environment variable, everything works as expected! It's an exciting workaround:
os.environ["VLC_PLUGIN_PATH"] = "/usr/lib64/vlc/plugins"
Problem is: I dont know where my vlc plugins are... So I cant test this.
Does anybody have ideas either for other ways to get around this problem, or to find the path of my vlc plugings?
FYI: My script works perfectly when I dont put it through PyInstaller.
link to the website with the info:
https://github.com/pyinstaller/pyinstaller/issues/4506

Different sound playing modules not working

I created a program for my little sister to learn math and now want to add sound. So looked up on how to add sound to my program and found the winsound module. I wrote this code:
import winsound
winsound.PlaySound("victory.wav", winsound.SND_FILENAME)
stopper = input("Input something to stop the program!")
But for some reason it only plays the default windows sound. (Bliiiiiingg)
The file victory.wav is located in the same folder as the python script. Then I saw someone else having the same problem as me and he said that adding the full path solved the problem. So I did that:
winsound.PlaySound(r"C:\Users\Nutzer\Desktop\gui\victory.wav", winsound.SND_FILENAME)
But it still didn't work. So I tried different methods of adding the path:
winsound.PlaySound("C:/Users/Nutzer/Desktop/gui/victory.wav", winsound.SND_FILENAME)
# and
winsound.PlaySound("C:\\Users\\Nutzer\\Desktop\\gui\\victory.wav", winsound.SND_FILENAME)
Still not working. Then I decided to switch modules, so I installed the playsound module and wrote the following code:
import playsound
playsound.playsound("victory.wav",block=True)
It said that it could not play the file, which means that it found the file. We're making progress.
So I changed the file extention to .mp3 and added the full path again:
playsound._playsoundWin("C:\\Users\\Nutzer\\Desktop\\gui\\victory.mp3",block=True)
Now it said something different:
Error 277 for command:
open "C:\Users\Nutzer\Desktop\gui\victory.mp3"
Fehler beim Starten von MCI.
Error 305 for command:
close "C:\Users\Nutzer\Desktop\gui\victory.mp3"
Zusätzliche Zeichen nach einer Zeichenkette mit Anführungszeichen sind nicht erlaubt.
Failed to close the file: "C:\Users\Nutzer\Desktop\gui\victory.mp3"
Traceback (most recent call last):
File "C:\Users\Nutzer\Desktop\gui\wer_support.py", line 3, in <module>
playsound._playsoundWin("C:\\Users\\Nutzer\\Desktop\\gui\\victory.mp3",block=True)
File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python39\lib\site-packages\playsound.py", line 72, in _playsoundWin
winCommand(u'open {}'.format(sound))
File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python39\lib\site-packages\playsound.py", line 64, in winCommand
raise PlaysoundException(exceptionMessage)
playsound.PlaysoundException:
Error 277 for command:
open "C:\Users\Nutzer\Desktop\gui\victory.mp3"
Fehler beim Starten von MCI.
Translated it means "Error starting MCI".
Then I decided to install pygame and use the mixer libary:
import pygame.mixer as mixyy
mixyy.init()
mixyy.music.load("victory.mp3")
mixyy.music.play(loops=0)
input = input("Press enter to stop the program.")
Surprisingly, that actually worked. But it caused the compiled file size to go from 16MB to 70MB and the compiled file could not even open.
So I scrapped that idea and found a module called mp3play, and copied the example code, and adjusted it for me:
import mp3play
filename = r'C:\Users\Nutzer\Desktop\gui\victory.mp3'
clip = mp3play.load(filename)
clip.play()
import time
time.sleep(min(30, clip.seconds()))
clip.stop()
Now it said this:
Traceback (most recent call last):
File "C:\Users\Nutzer\Desktop\gui\wer_support.py", line 1, in <module>
import mp3play
File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python39\lib\site-packages\mp3play\__init__.py", line 4, in <module>
from .windows import AudioClip as _PlatformSpecificAudioClip
File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python39\lib\site-packages\mp3play\windows.py", line 27
print 'Error %s for "%s": %s' % (str(err), txt, buf)
^
SyntaxError: invalid syntax
I don't see anything wrong with the syntax, as I literally copied the example from their documentation.
I just can't seem to get anything to work.
Things I also tried:
Using a different file.
Using various filetypes.
Reinstalling windows.
Trying on linux.
Restarting my computer.
Trying on a different computer.
Trying on mac.
Using a different python interpreter.
Using a different code visualizer (VSC, Pycharm, Idle).
Could somebody please tell me how to fix this?
You actually asked a lot of questions at the same time, and it might have been better to focus on one each time. However, since a number of questions are related, I'll try to answer them here:
winsound
At my system, winsound seems to be working fine. The following code works for me:
import winsound
winsound.PlaySound('victory.wav', winsound.SND_FILENAME)
However, the audio file must be a valid WAVE file (not an MP3 file or another audio format) that is supported by the winsound module. If it's not supported, it will play the system default sound (which you describe as Bliiiiiingg). This can be avoided by passing the winsound.SND_NODEFAULT flag:
import winsound
winsound.PlaySound('victory.wav', winsound.SND_FILENAME | winsound.SND_NODEFAULT)
In this case, when trying to play an invalid file format, you will get the error RuntimeError: Failed to play sound.
Unfortunately, the error message does not specify the cause of the problem. You will get the same error when passing an invalid path as when passing a valid path to an unsupported file. However, generally Windows applications do support forward slashes ('/') as well as backslashes ('\\'). In case of doubt, temporarily copy the file to the same directory as your script and remove the directory part of the path at all, so you can identify the problem.
Note that the WAV format is a container format, which supports different encodings. It is possible that not all formats/encodings are supported, so you just have to try which one works or not.
playsound
I also tried playsound, which did work for some of my WAV and MP3 files, but not for all of them. If it didn't work, I got one or more of the following errors:
playsound.PlaysoundException:
Error 259 for command:
play sample.mp3 wait
The driver cannot recognize the specified command parameter.
playsound.PlaysoundException:
Error 263 for command:
open sample.mp3
The specified device is not open or is not recognized by MCI.
playsound.PlaysoundException:
Error 277 for command:
open "sample.mp3" alias playsound_0.9221509684918991
A problem occurred in initializing MCI.
There are several bug reports at GitHub (see issues #36, #83, #113, #120 and #121). There have also been questions about this on Stack Overflow already:
"A problem occurred in initializing MCI" playsound issues
a problem occurred in initializing MCI python
why python modules playsound is not working?
Not able to play audio file in python
To me it seem that playsound has some problems with certain file formats. Possible solutions / work-arounds include:
Downgrade to playsound 1.2.2 (python -m pip install playsound==1.2.2) -- This was mentioned somewhere and I noticed that some files can indeed by played using 1.2.2 that cannot be played using 1.3.0, but it still doesn't play all my files.
Recode the MP3 file using e.g. Audacity -- This didn't work for my files.
Save the file in another format (e.g. convert MP3 to WAV) -- This didn't work for my files.
Note that arbitrarily changing file extensions is never a good idea. Please make sure what the type of the file is, and make sure it has the correct extension. Renaming an WAV file to '.mp3' or vice versa does not magically make it playable.
pygame
The same pygame code worked for me as well. I'm not sure why 70 MB is a problem, or what framework you use to compile the code (I assume py2exe or something similar). If this really is a problem, you might ask another question about this. There might be a way to decrease the size.
mp3play
The mp3play module seems to be heavily outdated and not maintained anymore. Its website is dead, and the last release on pypi was in 2008. The code is written in Python 2.x, and that's the reason you get the syntax error. (The syntax error is not in your code, but in the library.)
TL;DR
winsound works fine with supported filetypes (WAV only).
playsound seems to work fine with some WAV or MP3 files only.
pygame works fine as well. Not sure why size is an issue, but there might be solution for that.
mp3play is outdated, don't use it.
don't arbitrarily change file extensions.

I am able to make exe of my pyttsx3 file but i am unable to send my exe

I had worked to make A.I Base assistant and i was successful to make exe file but i am unable to send exe files to others as this error comes "missing pyttsx3 drivers ".I was successful to make it exe by writing this code in my spec file enter image description here
but when i send it to my other laptop same error comes"missing pyttsx3 drivers"
If you are converting using PyInstaller, then:
First You have to go to C:\Users\YourUser\AppData\Local\Programs\Python\Python39\Lib\site-packages\PyInstaller\hooks and search for hook-pyttsx3.py If found try again. If not found, then create a python file named hook-pyttsx3.py then write
hiddenimports = [
'pyttsx3.drivers',
'pyttsx3.drivers.dummy',
'pyttsx3.drivers.espeak',
'pyttsx3.drivers.nsss',
'pyttsx3.drivers.sapi5', ]
Video Tutorial: https://www.youtube.com/watch?v=GFMZyHi28oM
Thank You,

Converting .py to .exe & script failed to execute error

I am currently coding a game in python using pygame. I wanted to convert the game to a .exe to be able to share it with my class and this is my first time using auto-py-to-exe and I am not sure how to deal with this error. I followed the instructions and converted my .py file to a .exe file. When I opened the directory to run the executable, a window pops up and says "failed to execute script Game" (i called the scrip Game). Could someone please help me figure out what I am doing wrong?

Python Zelle graphics - image error

Using a mac, I am trying to set the background of my program to an image that i downloaded, I called the image "ChristmasBackground.jpg" and saved it to my documents. Using Zelle graphics, this is what I wrote:
XmasBack = Image(Point(600,100),"ChristmasBackground.jpg")
XmasBack.draw(win)
but the program is giving me an error saying:
couldn't open "ChristmasBackground.jpg": no such file or directory
just put it in the same folder of your script or write the file path

Categories

Resources