Error when using "playsound" library with pyinstaller - python

I'm trying to use pyinstaller to create an executable for my project, but when it hits the "playsound" function, outputs the error below. This only happens with the executable and not the regular code.
Error 263 for command:
open success.mp3
The specified device is not open or is not recognized by MCI.
Error 263 for command:
close success.mp3
The specified device is not open or is not recognized by MCI.
Failed to close the file: success.mp3
Exception in Tkinter callback
Traceback (most recent call last):
File "tkinter\__init__.py", line 1892, in __call__
File "Main.py", line 137, in <lambda>
File "Main.py", line 122, in loop
File "Main.py", line 109, in loopOver
File "playsound.py", line 72, in _playsoundWin
File "playsound.py", line 64, in winCommand
playsound.PlaysoundException:
Error 263 for command:
open success.mp3
The specified device is not open or is not recognized by MCI.
This is the code running playsound:
from playsound import playsound
playsound('success.mp3', False)
the file structure looks like this:
and this is the command i'm using to compile:
pyinstaller main.spec which has all modifications I need inside the .spec file
I'll be honest i'm completely stumped by this so I was hoping someone may have run into something similar in the past and can help me out? Thanks in advance!

To fix this, I implemented this code that I found from here to get my audio to load with pyinstaller:
def resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
playsound(resource_path("success.mp3"), False)

Related

exe generated by pyinstaller throws 'Permission denied' error

I write a python script to process some data in excel file and I use tkinter to generate a UI to input some paramaters, including input file path and output file path.
I use pyinstaller to package the script into exe. When I run the exe and input the paramaters in the UI, the following error appears:
Exception in Tkinter callback
Traceback (most recent call last):
File "tkinter\__init__.py", line 1892, in __call__
File "ut_calculate_tool.py", line 130, in calculate_data
File "styleframe\style_frame.py", line 318, in ExcelWriter
File "pandas\io\excel\_openpyxl.py", line 52, in __init__
File "pandas\io\excel\_base.py", line 925, in __init__
File "pandas\io\common.py", line 711, in get_handle
PermissionError: [Errno 13] Permission denied: 'C:/Users/XinDong/Documents/UT Calculate'
The path 'C:/Users/XinDong/Documents/UT Calculate' is the output path I typed into the UI.
Does anyone know how to deal with the bug? Thanks in advance.
Cause of the error is the logic of my code, not pyinstaller, I should pass a file name to the backend, not a folder...

Having an issue running Python Code in terminal

Hi everyone so I have some python code I am trying to run from my terminal (will not work in an IDE) to take a .mp3 file and classify the genre of the song based on the Spectrogram that we are using the librosa library plugin for. The code is from this gitHub: https://github.com/cetinsamet/music-genre-classification .When I use the command prompt specified by the gitHub user who created this app I get this error in my terminal:
(base) Nicos-MacBook-Pro:src nico$ python3 get_genre.py ../test.mp3
Traceback (most recent call last):
File "get_genre.py", line 61, in <module>
main(sys.argv[1:])
File "get_genre.py", line 30, in main
net.load_state_dict(torch.load(MODELPATH, map_location='cpu'))
File "/Users/nico/opt/anaconda3/lib/python3.7/site-packages/torch/serialization.py", line 419, in load
f = open(f, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: '../utils/net.pt'
Here is the command line specified: $ python3 get_genre.py ../test.mp3
The error message quite plainly tells you that the code depends on having a file ../utils/net.pt

Python Pyinstaller GUI Tkinter Selenium

I did not know how to create an executable python program before I asked here. Thankfully I received a fast answer and was able to convert my script to an executable program. The executable works perfect but only on my computer.
These are the two error's I am receiving, I feel like I need to modify the script in order to locate the chrome driver I am not sure where Pyinstaller saved everything.
Exception in Tkinter callback
Traceback (most recent call last):
File "site-packages\selenium\webdriver\common\service.py", line 76, in start
File "subprocess.py", line 775, in __init__
File "subprocess.py", line 1178, in _execute_child
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "tkinter\__init__.py", line 1705, in __call__
File "MarijuanaDoctors.py", line 25, in search
File "site-packages\selenium\webdriver\chrome\webdriver.py", line 68, in __init__
File "site-packages\selenium\webdriver\common\service.py", line 83, in start
selenium.common.exceptions.WebDriverException: Message: 'chromedriver'
executable needs to be in PATH. Please see
https://sites.google.com/a/chromium.org/chromedriver/home
You can bundle your "chromedriver.exe" along with your script using Pyinstaller like this:
pyinstaller --add-binary="localpathtochromedriver;." myscript.py
This will copy the "chromedriver.exe" file in the same folder as your main .exe(Or in case of single file option of pyinstaller, this fill will be extracted in temp folder while using exe program).
In your script you can check if you are running the script normally or from bundled(exe file) mode, and choose path to chromedriver.exe accordingly.(This change in script can be common for Single file/folder bundle option of pyinstaller)
import sys
if getattr(sys, 'frozen', False ):
#Running from exe, so the path to exe is saved in sys._MEIPASS
chrome_driver = os.path.join(sys._MEIPASS, "chromedriver.exe")
else:
chrome_driver = 'localpathtochromedriver.exe'
driver = webdriver.Chrome(executable_path=chrome_driver)
You can read about this in docs here.
Limitation:
The user of your .exe should have Chrome installed on their system and Chrome version should work with the chromedriver which is bundled.

Pyinstaller says that file already exists, but it doesn't

Ok, so I'm trying to make a little python script into an exe file, and I'm using pyinstaller. (My command I'm using is pyinstaller.exe --windowed --icon=pics\icon.ico -F --onefile main.py) I've figured out how to use pyinstaller, and it's worked before when I was making earlier versions of this python program. Now it isn't working, and I don't know why. It's saying that a file that it's trying to make already exists, except it doesn't (I've checked), and it's giving me this error:
765 INFO: PyInstaller: 3.4
765 INFO: Python: 3.7.1
765 INFO: Platform: Windows-10-10.0.17134-SP0
Traceback (most recent call last):
File "C:\Users\username\AppData\Local\Programs\Python\Python37\Scripts\pyinstaller-script.py", line 11, in <module>
load_entry_point('pyinstaller==3.4', 'console_scripts', 'pyinstaller')()
File "c:\users\username\appdata\local\programs\python\python37\lib\site-packages\pyinstaller-3.4-py3.7.egg\PyInstaller\__main__.py", line 109, in run
spec_file = run_makespec(**vars(args))
File "c:\users\username\appdata\local\programs\python\python37\lib\site-packages\pyinstaller-3.4-py3.7.egg\PyInstaller\__main__.py", line 56, in run_makespec
spec_file = PyInstaller.building.makespec.main(filenames, **opts)
File "c:\users\username\appdata\local\programs\python\python37\lib\site-packages\pyinstaller-3.4-py3.7.egg\PyInstaller\building\makespec.py", line 331, in main
os.makedirs(specpath)
File "c:\users\username\appdata\local\programs\python\python37\lib\os.py", line 221, in makedirs
mkdir(name, mode)
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'C:\\Users\\username\\OneDrive\\Documents\\Game Center'
(It's done this before, but that time, the file I was trying to make DID exist already. I deleted the file, tried again, and it all worked fine.)
Any help is appreciated. Thanks!
I just figured it out. I don't know what changed, but it works just fine now. I just waited a week and came back to it. I guess God just sent a miracle!

error trying to load exiftool

I have installed exiftool (https://smarnach.github.io/pyexiftool/) and I am able to import the library, but I get the following error when trying to run the test data just to see if it works.
ERROR: test_get_metadata (__main__.TestExifTool)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Program Files\Python36\Lib\site-
packages\pyexiftool\test\test_exiftool.py", line 66, in test_get_metadata
with self.et:
File "C:\Program Files\Python36\lib\site-packages\exiftool.py", line 191, in __enter__
self.start()
File "C:\Program Files\Python36\lib\site-packages\exiftool.py", line 174, in start
stderr=devnull)
File "C:\Program Files\Python36\lib\subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "C:\Program Files\Python36\lib\subprocess.py", line 997, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
I also did run the setup code that is in the exiftool folder and still no luck. I think it might be a library issue or path or the (init.py) file, but I've tried several ways and so I'm here to ask if anyone else has a solution or ideas for me to try and fix it.
I'm running Python 3.6.6 and have tried other versions.
(I can run exiftool in command line, but I have encoded BASE64 images that exiftool doesnt work in command line to fully decode.)
Thank you StarGeek! The problem was that I didn't have the exiftool command tool (the separate application of exiftool) in the right PATH env variable. Once I added the application to the PATH env variables i got it to work. Also in the python exiftool code at line 70 it says that you have to have it in the path or direct it to the executable, that I had missed. Thanks again!

Categories