Failed to play an audio file with Python's playsound - python

I tried to use the playsound module to play an audio file. When I run my code I get this error:
Traceback (most recent call last):
File "/Users/Tarantino/Desktop/CodeTesting/sound.py", line 3, in <module>
playsound('sound.mp3')
File "/Users/Tarantino/Library/Python/3.8/lib/python/site-packages/playsound.py", line 67, in _playsoundOSX
raise IOError('Unable to load sound named: ' + sound)
OSError: Unable to load sound named: file:///Users/Tarantino/Desktop/Code Testing/sound.mp3
I have the audio file in the same folder as the Python code, could I please get help on what to do?

Move file you're playing to folder that has no special or non-ascii characters on the full path from root of disk, spaces in files/dirs names are not allowed too. Because playsound is creating an URL from full path-location of file and this URL should has valid for URLs chars.
E.g. /x/y/z.mp3 path is alright, but /x y/z.mp3 and /x[y]/z.mp3 and /x/漢/z.mp3 are not.
Also try upgrading library via python -m pip install --upgrade playsound, I've just installed library and tried playing sound and it plays for me.
Also some dir on the path might not have read permissions for the user you are running python with.

Related

File not found in working directory pygame

I am attempting to make a simple pygame game, and I need to load in some images. When I run my code, or:
bg_image = pygame.image.load('assets/Fondotiff.tif(1).png').convert_alpha()
It gives me an error which says:
Traceback (most recent call last):
File "C:\Users\victo\Desktop\Assets - Pulga\mainpulga.py", line 12, in <module>
bg_image = pygame.image.load('assets/Fondotiff.tif(1).png').convert_alpha()
FileNotFoundError: No file 'assets/Fondotiff.tif(1).png' found in working directory 'C:\Users\victo\Desktop\Assets - Pulga'.
***Repl Closed***
I have double checked if the image is inside the folder on my desktop and it is there, but for some reason it doesn't find it.
How can I fix this?
The asset file path has to be relative to the current working directory. The working directory is possibly different from the directory of the python file.
It is not enough to put the files in the same directory or sub directory. You also need to set the working directory.
e.g.:
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))

How to install pdfkit (python module) in windows 10?

I install pdfkit using pip (pip install pdfkit).
Then i install wkhtmltopdf from here
But when i try to run the following code:
import pdfkit
config = pdfkit.configuration(wkhtmltopdf='C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe')
pdfkit.from_url('https://www.google.com', 'C:\\Users\\Χρήστος\\Desktop\\out-test.pdf', configuration=config)
An error oqqurs:
Traceback (most recent call last):
File "create_pdf.py", line 4, in <module>
pdfkit.from_url('https://www.google.com', 'C:\\Users\\Χρήστος\\Desktop\\out-test.pdf', configuration=config)
File "C:\Users\Χρήστος\AppData\Local\Programs\Python\Python38\lib\site-packages\pdfkit\api.py", line 26, in from_url
return r.to_pdf(output_path)
File "C:\Users\Χρήστος\AppData\Local\Programs\Python\Python38\lib\site-packages\pdfkit\pdfkit.py", line 156, in to_pdf
raise IOError('wkhtmltopdf reported an error:\n' + stderr)
OSError: wkhtmltopdf reported an error:
Loading pages (1/6)
QPainter::begin(): Returned false============================] 100%
Error: Unable to write to destination
Exit with code 1, due to unknown error.
Any advice would be useful.
I also run the code with administration priviledges.
Edit: From wkhtmltopdf site:
How do I use it? Download a precompiled binary or build from source
Create your HTML document that you want to turn into a PDF (or image)
Run your HTML document through the tool. For example, if I really like
the treatment Google has done to their logo today and want to capture
it forever as a PDF:
wkhtmltopdf http://google.com google.pdf
So i tried this:
cd C:\Program Files\wkhtmltopdf\bin
wkhtmltopdf http://google.com google.pdf
Didn't work, but when I run it with admininstration priviledges the pdf was make.
So the pdfkit module must open wkhtmltopdf with admin priviledges.
There's an issue on GitHub, seems you have to specify the full path (eg. 'C://foo/bar')
Probably because the script doesn't have permission to write in the folder where it lives:
C:\Users\Χρήστος\AppData\Local\Programs\Python\Python38\lib\site-packages\pdfkit\pdfkit.py
You gave it a relative path, so in runtime, it will try to create a file:
C:\Users\Χρήστος\AppData\Local\Programs\Python\Python38\lib\site-packages\pdfkit\out.pdf

Struggling with making a Python module accessible via PyPi

I wrote a simple Python interface for working with the Pushwoosh notification service a while back, which is at https://github.com/Astutech/Pushwoosh-Python-library and I've finally gotten around to publishing it so it can be installed using Pip. This is the first time I've published a Python library to PyPi and I'm in a bit of a muddle.
Trying to install it brings up the following error:
Collecting pushwoosh
Using cached pushwoosh-1.0.0.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 20, in <module>
File "/tmp/pip-build-5m3jj7uu/pushwoosh/setup.py", line 17, in <module>
with open(path.join(here, 'DESCRIPTION.rst'), encoding='utf-8') as f:
File "/usr/lib/python3.4/codecs.py", line 896, in open
file = builtins.open(filename, mode, buffering)
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/pip-build-5m3jj7uu/pushwoosh/DESCRIPTION.rst'
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-5m3jj7uu/pushwoosh
It looks like some kind of path related issue. I'm not sure that pushwoosh.py is in the correct location. But if I move it to pushwoosh/pushwoosh.py, and add a pushwoosh/__init__.py fileI then need to import it as follows:
from pushwoosh.pushwoosh import Pushwoosh
This is obviously not ideal. From the documentation I can't see where I've gone wrong. What directory layout should I be using?
EDIT: I have now resolved the issue with DESCRIPTION.rst, but unless I move the pushwoosh.py file to a pushwoosh folder and add an __init__.py file, installing the library doesn't actually install pushwoosh.py so it doesn't work. How can I amend it so I can import it like this?
from pushwoosh import Pushwoosh
Figured it out in the end. It works if you put the file inside the module_name folder as __init__.py.
You could've just put this into your __init__py file.
from pushwoosh import Pushwoosh
This lets you make the import like this without packing everything into the init.py file itself.
from pushwoosh import Pushwoosh
for a nice explanation check out this post http://mikegrouchy.com/blog/2012/05/be-pythonic-__init__py.html

Trouble playing an MP3 file in python via Pyglet

Summary: (Trying to play MP3 in python via pyglet library, AVbin not detected, Mp3 not played)
So i'm trying to to play an MP3 file in python with pyglet. i installed pyglet with the command prompt and all that good stuff, and I went to the AVbin website and downloaded and ran their .exe file. Im not sure if just running the .exe file actually installs AVbin, but that's all ive done with it so far. So my problem is im trying to play an mp3 that i have located on my desktop, but Pyglet still doesnt recognize AVbin? Here's the code as well as the error message:
Code:
import pyglet
music = pyglet.media.load(r"C:\Users\Doug\Desktop\01 Gem Shards - 4A.mp3")
music.play()
pyglet.app.run()
Error:
Traceback (most recent call last):
File "C:/Python34/test.py", line 3, in <module>
music = pyglet.media.load(r"C:\Users\Doug\Desktop\01 Gem Shards - 4A.mp3")
File "C:\Python34\lib\site-packages\pyglet-1.2.3a1-py3.4.egg\pyglet\media\__init__.py", line 1429, in load
source = get_source_loader().load(filename, file)
File "C:\Python34\lib\site-packages\pyglet-1.2.3a1-py3.4.egg\pyglet\media\__init__.py", line 1410, in load
return riff.WaveSource(filename, file)
File "C:\Python34\lib\site-packages\pyglet-1.2.3a1-py3.4.egg\pyglet\media\riff.py", line 201, in __init__
'AVbin is required to decode compressed media')
pyglet.media.riff.WAVEFormatException: AVbin is required to decode compressed media
Add AvBin to your PATH enviroment variable:
you can do this with the folowwing command
setx path "%path%;c:\pathToAvBin"
plase note that you need to change "c:\pathToAvBin" for your real path to av bin :)
BE CAREFULL TO NOT CLEAR YOUR PATH

Difficulties installing SOAPpy module

I am very new to python and have been figuring out how to install modules.I have been trying to install the SOAPpy module and have only had success with the required modules fpconst, wstools, and setuptools(not mentioned as required, but still ended up being required).
I am getting an error when I try and install the SOAPpy module.
Traceback <most recent call last>:
File "...\setup.py", line 43, in module <module>
__version__ = load_version()
File "...\setup.py", line 35 in load_version
execfile(filename, d.__dict__)
IOError: [Errno 2] Unable to load the version number (no such file or directory):
'...\\src\\SOAPpy\\version.py'
Not sure what i should do to fix this.
Any help is greatly appreciated!
Looking at SOAPpy's setup.py file, it tries to import SOAPpy.version. This doesn't work if you're calling it from somewhere else. Try running it directly in the SOAPpy directory because Python adds your current working directory to sys.path. For example:
cd C:\Users\eclaird\Download\SOAPpy\ # The folder with setup.py
python.exe setup.py install
Thanks for the help.
I eventually found what the issue was. I had to get some other required modules for the install to work, also the tar file I got was missing directories, so I found the completed version on Github.

Categories

Resources