How to make vscode work with playsound module? - python

I have this very simple test code:
from playsound import playsound
playsound('alarmsound.wav')
alarmsound.wav is a file in the same directory. when I run it in a terminal, it works fine. But when I run it in VSCode, it blows up with the following message:
Traceback (most recent call last):
File "c:/Users/Luca/Desktop/Python/own/playaudio.py", line 3, in <module>
playsound('alarmsound.wav')
File "C:\Users\Luca\AppData\Local\Programs\Python\Python38-32\lib\site-packages\playsound.py", line 35, in _playsoundWin
winCommand('open "' + sound + '" alias', alias)
File "C:\Users\Luca\AppData\Local\Programs\Python\Python38-32\lib\site-packages\playsound.py", line 31, in winCommand
raise PlaysoundException(exceptionMessage)
playsound.PlaysoundException:
Error 275 for command:
open "alarmsound.wav" alias playsound_0.7197283376226026
Cannot find the specified file. Make sure the path and filename are correct.
It used to work in vscode before, now suddenly it blows up. what could be wrong?

Make sure you're actually running the file from the directory where your sound file is in.
In my case, it was running Python from the parent directory of the folder with the .wav file.

Related

I cannot open my EXE File on other Desktop

I finished my code on Pycharm. Now I want to convert my .py file into .exe
After i converted it. The application file are running on the desktop (desktop1) that I used to convert, but when i try to open the application on other desktop (desktop2) it has an error that says.
`Traceback (most recent call last):
File "main.py", line 220, in <module>
obj = DocumentsManagementSystem(root)
File "main.py", line 20, in __init__
self.root.iconbitmap(r'C://Users//HP//PycharmProjects//pythonProject1//Images//senglogo.ico')
File "tkinter\__init__.py", line 2109, in wm_iconbitmap
_tkinter.TclError: bitmap "C://Users//HP//PycharmProjects//python`
but when i open it on the desktop (desktop1) it is running. That's the issue. I cannot open it on other desktop. If you have any idea on how i can solve this. I will gladly do it. Thank you so much!
Use pyinstaller with additional file and add-data parameters. In your case use this command:
pyinstaller --add-data '//Users//HP//PycharmProjects//pythonProject1//Images//senglogo.ico:.' script.py
It seems like you missed adding an additional and data file in your pyinstaller process.
if you're beginner you can use the GUI of pytinstaller.
https://pypi.org/project/auto-py-to-exe/
The required steps for py to exe.
Analysis: Finding the Files Your Program Needs
https://pyinstaller.org/en/stable/operating-mode.html#analysis-finding-the-files-your-program-needs
Add required file by using this command:
pyinstaller --add-data 'src/README.txt:.' myscript.py

What is the error in the code for this playsound module even though the syntax is same as on official site?

I have checked path variables too everything works fine for other functions but it is showing error for this module only.
Code:
from playsound import playsound
playsound('E://PYTHON//Music.mp3')
The following is the error:
PS C:\Users\HP> & C:/Users/HP/AppData/Local/Programs/Python/Python39/python.exe e:/PYTHON/Playsound.py
Error 263 for command:
open E://PYTHON//Music.mp3
The specified device is not open or is not recognized by MCI.
Error 263 for command:
close E://PYTHON//Music.mp3
Failed to close the file: E://PYTHON//Music.mp3
Traceback (most recent call last):
File "e:\PYTHON\Playsound.py", line 2, in <module>
playsound('E://PYTHON//Music.mp3')
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\playsound.py", line 72, in _playsoundWin
winCommand(u'open {}'.format(sound))
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\playsound.py", line 64, in winCommand
raise PlaysoundException(exceptionMessage)
playsound.PlaysoundException:
Error 263 for command:
open E://PYTHON//Music.mp3
The specified device is not open or is not recognized by MCI.
PS C:\Users\HP> & C:/Users/HP/AppData/Local/Programs/Python/Python39/python.exe e:/PYTHON/Playsound.py
Error 263 for command:
open E://PYTHON//Music.mp3
The specified device is not open or is not recognized by MCI.
Error 263 for command:
close E://PYTHON//Music.mp3
The specified device is not open or is not recognized by MCI.
Failed to close the file: E://PYTHON//Music.mp3
Traceback (most recent call last):
File "e:\PYTHON\Playsound.py", line 2, in <module>
playsound('E://PYTHON//Music.mp3')
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\playsound.py", line 72, in _playsoundWin
winCommand(u'open {}'.format(sound))
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\playsound.py", line 64, in winCommand
raise PlaysoundException(exceptionMessage)
playsound.PlaysoundException:
Error 263 for command:
open E://PYTHON//Music.mp3
The specified device is not open or is not recognized by MCI.
pip install playsound==1.2.2
The specified device is not open or is not recognized by MCI - playsound 1.3.0
I use Python 3.8.8 under Windows 10.
My audio file is D:\my\path\to\wav\mysound.wav
playsound ('mysound.wav') resulted in the 'The specified device is not open..' error mentioned above.
I tried to supply the full path, but later realized that os.path.dirname(__file__) returns a null string.
os.getcwd() returned the correct directory, but ran into a different issue:
When I spliced the directory name with the file name, I got D:\my\path\to\wav\mysound.wav, but this also did not work with playsound. I got the error: 'Cannot specify extra characters after a string enclosed in quotation marks.'
Finally, I hardcoded the entire path using forward slashes: D:/my/path/to/wav/mysound.wav
Now playsound() worked without issues.
This is only a quick and dirty solution, since my project is an experimental one. But the point I am trying to make is, playsound() is finicky with file names.
I found the solution that we need to add the file to the same folder in which we have our current python program so we do need to add the path for the file
from playsound import playsound
playsound('Music.mp3')
In my case I simply use playsound('./' + audio_filename) because the audio and the .py file were in the same path or directory. You can use ../ to step back.

Adding pip libraries in pyinstaller to create an exe file for my script

I am able to run my exe file but my scripts has multiple pip libraries for it to work. I always get this error when running my script.
Exception in thread Thread-1:
Traceback (most recent call last):
File "threading.py", line 932, in _bootstrap_inner
File "threading.py", line 870, in run
File "AIO_Bot.py", line 265, in main_function
File "cloudscraper/__init__.py", line 665, in create_scraper
File "cloudscraper/__init__.py", line 120, in __init__
File "cloudscraper/user_agent/__init__.py", line 20, in __init__
File "cloudscraper/user_agent/__init__.py", line 77, in loadUserAgent
FileNotFoundError: [Errno 2] No such file or directory: '/var/folders/8g/gkf53znx7_7405c26w344ftw0000gn/T/_MEIujM2ll/cloudscraper/user_agent/browsers.json'
Finished in 0.0 second(s)
If anyone knows how to fix this please help!
It seems that your browswers.json is expected to be found in a relative path from the "exe" (cloudscraper/user_agent) when it is not the case when it is really a exe file. You have to modify your pyinstaller build command to include that file in the package. Add
--add-data '/lib/python3.8/site-packages/cloudscraper/user_agent/browsers.json:cloudscraper/user_agent' and it should work. (replace : with ; on Windows)
You can refer to https://pyinstaller.readthedocs.io/en/stable/spec-files.html#adding-data-files for more details.
It seems your script/exe is looking for a file named "browsers.json".
Locate it and try putting it in the same folder as your created *.exe
The file is not being located because you are searching through a path that can not be opened. If your script is on the same level of the path then you would only have to do this
(browsers.json)
to fix this just exclude the directory path
mv /lib/python3.8/site-packages/cloudscraper/user_agent/browsers.json /var/folders/8g/gkf53znx7_7405c26w344ftw0000gn/T/_MEIujM2ll/cloudscraper/user_agent/
if this does not work pip install cloudscraper in terminal instead because pycharms assigniing the file to it's own enviroment

Cannot access token.json: No such file or directory Python

I'm trying to access the Gmail API via Python. Here is the instruction.
I have copied the credentials.json (I have also renamed a copy of it and called it token.json and added it into the main folder) and the quickstart.py files in the main folder. I run the quickstart.py in the cmd and using Atom IDE, but I get:
C:\Users\HP Pavilion\AppData\Local\Programs\Python\Python36\lib\site-packages\oauth2client\_helpers.py:255: UserWarning: Cannot access token.json: No such file or directory
warnings.warn(_MISSING_FILE_MESSAGE.format(filename))
Traceback (most recent call last):
File "C:\Users\HP Pavilion\AppData\Local\Programs\Python\Python36\lib\site-packages\oauth2client\clientsecrets.py", line 121, in _loadfile
with open(filename, 'r') as fp:
FileNotFoundError: [Errno 2] No such file or directory: 'credentials.json'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\HP Pavilion\Documents\Atom\Gmail\quickstart.py", line 34, in <module>
main()
File "C:\Users\HP Pavilion\Documents\Atom\Gmail\quickstart.py", line 18, in main
flow = client.flow_from_clientsecrets("credentials.json", SCOPES)
File "C:\Users\HP Pavilion\AppData\Local\Programs\Python\Python36\lib\site-packages\oauth2client\_helpers.py", line 133, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Users\HP Pavilion\AppData\Local\Programs\Python\Python36\lib\site-packages\oauth2client\client.py", line 2135, in flow_from_clientsecrets
cache=cache)
File "C:\Users\HP Pavilion\AppData\Local\Programs\Python\Python36\lib\site-packages\oauth2client\clientsecrets.py", line 165, in loadfile
return _loadfile(filename)
File "C:\Users\HP Pavilion\AppData\Local\Programs\Python\Python36\lib\site-packages\oauth2client\clientsecrets.py", line 125, in _loadfile
exc.strerror, exc.errno)
oauth2client.clientsecrets.InvalidClientSecretsError: ('Error opening file', 'credentials.json', 'No such file or directory', 2)
Seems like the .json files can't be found. They are however in the same folders. I have also tried to run the code as an administrator but without any success.
I am not sure what happened to you but I run the script with either PyCharm or VS Code and it runs perfectly.
The only prerequisite (probably you skipped this step) is that you should first generate the credentials.json file, download it and put it into the same directory where you have the python script. In addition, I created an empty tokens.json file.
First, it will automatically open your default browser to indicate which Gmail account you want to specify. Next, it will output the labels of your Gmail account.
My mistake was that I right clicked on my JSON file, selected rename, then named it credentials.JSON, which made the real file name credentials.JSON.JSON.
If it is in the same location as the script, you can try:
import os
path = os.path.abspath('credentials.json')
with open(path,'r'):
//code
I had the same issue and it turned out that the problem was with the IDE I'm using (Mu 1.1.0). Once I downloaded the JSON file, renamed it, placed it in the directory to where the error was pointing to, and ran everything with cmd, it worked as it should.

python in notepad++ no such file or directory

python 2.7. Notepad++. Windows 10
I'm trying to open file like this:
sourcefile=raw_input('name of the file: ')
fhand=open(sourcefile,'r')
And it gives me this error:
Traceback (most recent call last):
File "C:\Users\Denis\Desktop\prog\python\es.py", line 135, in <module>
fhand=open(sourcefile)
IOError: [Errno 2] No such file or directory: '20.04.2016.csv'
In notepad++ i run this: C:\Python27\python.exe -i "$(FULL_CURRENT_PATH)"
Kinda new to programming but i'm guesing that I have to specify somehow path to the place where my es.py and my file is. I thought $(FULL_CURRENT_PATH) does it.
If i run my code with python or idle it works fine.
I've tried to run this program in Atom, gives same mistake. No mistakes so long as I don't work with files.

Categories

Resources