PyDub OSError: [WinError 6] The handle is invalid - python

I'm having a similar issue on Python 3.8.7, and I can't find the solution.
In my project, I'm using pydub.AudioSegment to get audio from a file and then exporting that audio in a different format. My code works perfectly when I'm running the python file directly. However, when I convert it to an executable with pyinstaller, run the program and get to the point of importing the audio with pydub, it gives me the following error:
Traceback (most recent call last):
File "main.py", line 269, in <module>
File "main.py", line 213, in convertfile
File "main.py", line 133, in cloud_upload
File "pydub\audio_segment.py", line 728, in from_file
File "pydub\utils.py", line 274, in mediainfo_json
File "subprocess.py", line 804, in __init__
File "subprocess.py", line 1142, in _get_handles
OSError: [WinError 6] The handle is invalid
pydub call in my program looks like this:
sound = AudioSegment.from_file(filepath)
sound.export(new_filepath, format="ogg",codec='libopus')
I've tried to add stdin=subprocess.DEVNULL and stdin=subprocess.PIPE in utils.py on line 274, but that didn't work either. Maybe I added them incorrectly, though, so suggestions like that are also highly appreciated.

I've managed to solve the problem only by removing --onefile option from pyinstaller and dropping ffmpeg.exe and ffprobe.exe into the resulted folder with the main.exe file.
That's not a good solution to the problem as I'd still prefer to use --onefile; but it works.
I'm still open to suggestions on how to make it work with --onefile or just generally why this is hapenning.

Chances are is that PyInstaller cannot recognize the imported plugin. Although if you are trying to make a app, I suggest using a shortcut instead, it is better due to the fact that you can customize the icon for the shortcut. It is what many apps mainly use.

Related

PyInstaller executable not working with panda3d. No graphics pipe availabe

I am having an issue with a program, which uses panda3d. It works perfectly when executed as a pythonscript, but the version, which is compiled (or rather packaged) by PyInstaller throws the exception below.
:display(warning): Unable to load libpandagl.so: No error.
Known pipe types:
(all display modules loaded.)
Traceback (most recent call last):
File "Shipsim3d_1-1.py", line 930, in <module>
File "Shipsim3d_1-1.py", line 23, in __init__
File "direct/showbase/ShowBase.py", line 339, in __init__
File "direct/showbase/ShowBase.py", line 1024, in openDefaultWindow
File "direct/showbase/ShowBase.py", line 1059, in openMainWindow
File "direct/showbase/ShowBase.py", line 769, in openWindow
File "direct/showbase/ShowBase.py", line 749, in <lambda>
File "direct/showbase/ShowBase.py", line 821, in _doOpenWindow
File "direct/showbase/ShowBase.py", line 650, in makeDefaultPipe
File "direct/directnotify/Notifier.py", line 130, in error
Exception: No graphics pipe is available!
Your Config.prc file must name at least one valid panda display
library via load-display or aux-display.
[5466] Failed to execute script 'Shipsim3d_1-1' due to unhandled exception!
Usually, there is a Config.prc file in the etc subdirectory of the panda3d site-package directory. This file is non-existent in the packaged version. But if I create this subdirectory there, and copy the files to it as well, it still doesn't work and it still throws the same exception. There is a line "loadPrcData("win-size 1080 1920")" in my main program as well. Could this be part of the problem in any way? Or is there something else i am missing?
Thanks in advance
I solved this problem by including the whole panda3d lib:
pyinstaller --add-data="path/to/panda3d;panda3d" -wF main.py

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

building matplotlib with pyinstaller 2.0

I am trying to use pyinstaller 2.0 to build an .exe file from a python script which uses matplotlib and numpy, but I keep failing. I tried this code from
Building python pylab/matplotlib exe using pyinstaller
as a simple reference, I get the build done for me, but when I run the exe I get the error:
Traceback (most recent call last):
File "<string>", line 12, in <module>
File "C:\Compilations\pyinstaller-2.0\PyInstaller\loader\iu.py", line 386, in
importHook
mod = _self_doimport(nm, ctx, fqname)
File "C:\Compilations\pyinstaller-2.0\PyInstaller\loader\iu.py", line 480, in
doimport
exec co in mod.__dict__
File "C:\Compilations\pyinstaller-2.0\ptest\build\pyi.win32\ptest\out00-PYZ.py
z\PIL.PpmImagePlugin", line 27, in <module>
LookupError: no codec search functions registered: can't find encoding
Is this a bug or do I need to tinker with the .spec file and link the matplotlib libraries to pyinstaller somehow? If so - can anyone show me a simple example, please?
I tried cx_Freeze before and also seem to have a problem as soon as I use a code with matplotlib.
Thanks in advance!
Edit PpmImagePlugin.py and add 'import encodings' (without the quotes) before any other imports. I had the same problem and this fixed it. Source: http://www.pyinstaller.org/ticket/651

pyinstaller and win32com

So I'm confused about how to get pyinstaller to build an application that uses win32com. I have a script that runs with no problem from IDLE or command line that interacts with excel via
xl = Dispatch('Excel.Application')
followed by some other really basic excel calls, then closes. I've seen advice to include "clsctx=pythoncom.CLSCTX_LOCAL_SERVER" as an argument to Dispatch, but it doesn't change anything. Whenever I build the script, I get an error similar to this
Traceback (most recent call last):
File "<string>", line 7, in <module>
File "C:\Python27\trunk\iu.py", line 436, in importHook
mod = _self_doimport(nm, ctx, fqname)
File "C:\Python27\trunk\iu.py", line 521, in doimport
exec co in mod.__dict__
File "Myskript\build\pyi.win32\Myskript\outPYZ1.pyz/win32com.client", line 11,
in <module>
File "C:\Python27\trunk\iu.py", line 436, in importHook
mod = _self_doimport(nm, ctx, fqname)
File "C:\Python27\trunk\iu.py", line 521, in doimport
exec co in mod.__dict__
File "Myskript\build\pyi.win32\Myskript\outPYZ1.pyz/win32com.client.gencache",
line 662, in <module>
File "Myskript\build\pyi.win32\Myskript\outPYZ1.pyz/win32com.client.gencache",
line 58, in __init__
File "Myskript\build\pyi.win32\Myskript\outPYZ1.pyz/win32com.client.gencache",
line 649, in Rebuild
File "Myskript\build\pyi.win32\Myskript\outPYZ1.pyz/win32com.client.gencache",
line 65, in _SaveDicts
File "Myskript\build\pyi.win32\Myskript\outPYZ1.pyz/win32com.client.gencache",
line 141, in GetGeneratePath
IOError: [Errno 22] invalid mode ('w') or filename: 'C:\\Python27\\trunk\\Myskri
pt\\dist\\Myskript.exe?844863\\__init__.py'
That's copied from this german question http://www.python-forum.de/viewtopic.php?f=1&t=25010 but my error is basically the same. I tried to implement the advice given on that page to no avail either.
I know next to nothing about COM stuff, just hoping to get this little excel app to run.
EDIT: Also tried importing pythoncom and running "CoInitialize()" (based on this guy Call MS Access module function from Python after compiling with py2exe fails saying that his script ran fine with pyinstaller) but that didn't change anything either. Really at a loss here and appreciate any advice.
I'm not sure if you are still using py2exe, i was about to try your workaround when i stumbled into this thread:
Call MS Access module function from Python after compiling with py2exe fails
The "solution" was using Pyinstaller that has a bunch of workarounds implemented on by default depending on your script. I tried it with my pywin32 script that uses excel and word com objects and it worked perfectly without any tweaks.

google drive api crash with desktop application

I use google drive application wrote a desktop application using python and everything works fine. But when I use pyinstaller to output a .exe file and run that application. A problem occurs on these lines:
if credentials is None or credentials.invalid:
credentials = run(FLOW, storage)
The authentication page shows and I allow its access. Unlike usual, there is no response after that and I found that the .exe program exits with no reason.
Anyone met this problem before? If so, how to solve it?
P.S. I traced the error now and it is as follows:
Traceback (most recent call last):
File "<string>", line 697, in <module>
File "<string>", line 562, in __init__
File "build\bdist.win32\egg\oauth2client\tools.py", line 166, in run
File "build\bdist.win32\egg\oauth2client\client.py", line 1069, in step2_exchange
File "USB\build\pyi.win32\USB\outPYZ1.pyz/httplib2", line 1544, in request
File "USB\build\pyi.win32\USB\outPYZ1.pyz/httplib2", line 1294, in _request
File "USB\build\pyi.win32\USB\outPYZ1.pyz/httplib2", line 1230, in _conn_request
File "USB\build\pyi.win32\USB\outPYZ1.pyz/httplib2", line 984, in connect
File "USB\build\pyi.win32\USB\outPYZ1.pyz/httplib2", line 80, in _ssl_wrap_socket
File "USB\build\pyi.win32\USB\outPYZ1.pyz/ssl", line 381, in wrap_socket
File "USB\build\pyi.win32\USB\outPYZ1.pyz/ssl", line 141, in __init__
ssl.SSLError: [Errno 185090050] _ssl.c:340: error:0B084002:x509 certificate rout
ines:X509_load_cert_crl_file:system lib
I saw someone encountered the similar error http://code.google.com/p/google-api-python-client/issues/detail?id=58 but the reply said it already fixed it.
I also tried the method in https://github.com/kennethreitz/requests/issues/557 but it is not working.
Does anyone know how to fix it?
I'm guessing, but this is possible related to STDIN handling on Windows exe from pyinstaller - usually this won't be available to you, so you might have to run your own custom flow.
But you can narrow it down to either:
Reading/writing from STDIN/STDOUT
Starting a local web server
Launching a browser
Since all those need to be performed when running a flow locally, and one of them is going wrong for you.
If you log STDERR to a text file, you will be able to see which part is crashing.
After digging a little bit, I found the solution based on solution provided by Dropbox api developer: https://forums.dropbox.com/topic.php?id=65523&replies=1#post-461457. This problem is basically caused by:
CA_CERTS = os.path.join(os.path.dirname(os.path.abspath(__file__ )), "cacerts.txt")
__file__ is the key that causes this problem. It cannot work normally in the executable program to find the path.
Similar problem can be found here: pyinstaller seems not to find a data file
In order to solve this problem, I change the above code into this:
CA_CERTS = os.path.join(os.path.dirname(sys.executable), "cacerts.txt")
By doing this, the .exe program will try to find cacerts.txt in the directory where .exe file is located. After compiling this into .pyc, I put cacerts.txt into .exe directory. Then the program can run normally.

Categories

Resources