Runtime ImportError in Python executable created using PyInstaller - python

I'm trying to get my feet wet with Python's asyncio module and have a simple script that runs fine but when I compile it using PyInstaller and run the executable it creates I get a runtime ImportError that I can't figure out how to fix.
I've spent several hours scouring the internet to see if anyone else has encountered the same issue but haven't been able to find anything and I don't understand what's causing the issue.
The script I'm compiling looks like this:
import requests
import asyncio
async def main():
loop = asyncio.get_event_loop()
futures = [
loop.run_in_executor(
None,
requests.get,
'http://google.com'
)
for i in range(5)
]
for response in await asyncio.gather(*futures):
print(response.status_code)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
The command I use to compile the script is:
pyinstaller -F async_test.py
And the runtime exception that is thrown when running the executable that PyInstaller creates is:
Traceback (most recent call last):
File "asyncio\__init__.py", line 18, in <module>
ImportError: cannot import name '_overlapped'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "Miscellaneous\async_test.py", line 2, in <module>
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "c:\users\username\appdata\local\programs\python\python36\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
exec(bytecode, module.__dict__)
File "asyncio\__init__.py", line 20, in <module>
OSError: [WinError 10022] An invalid argument was supplied
[13048] Failed to execute script async_test
I'm using Python 3.6.4 and PyInstaller 3.4
If someone could shed some light on what the problem is and how to fix it I'd be very grateful.

Related

Circular import error only after turning .py into .exe

I am working on building a GUI interface that can be ran as an executable. After using auto-py-to-exe to turn the file into an exe, I try to run it and I receive an error.
Traceback (most recent call last):
File "CarrieGUI.py", line 3, in <module>
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
File "requests.py", line 8, in <module>
AttributeError: partially initialized module 'requests' has no attribute 'get' (most likely due to a circular import)
I have seen other posts suggesting that the file name is what causes this error but I do not receive this error when running it from vscode. Plus the file name is CarrieGUI.py which should not be something referred to in the requests library. Any ideas on why I am only running into this error after converting the file to an exe?

how do i fix debugpy syntax error when using py2exe to convert a py file to and exe?

I am trying to convert a py file to an exe file but there seems to be a few syntax errors inside debugpy. I want to use the exe file to edit word documents and as i understood pyinstaller for example dont work with that. So im trying to use py2exe.
From what i can see inside the file, there seems to be multiple syntax errors and i tried to fix them manually but that didnt seems to work either.
Is there something wrong with debugpy itself or is it any other programs that clashes with it?
Its also starts with an exception:
Start:
C:\Python_3.9\lib\site-packages\py2exe\mf34.py:258: VisibleDeprecationWarning: zmq.eventloop.minitornado is deprecated in pyzmq 14.0 and will be removed.
Install tornado itself to use zmq with the tornado IOLoop.
return self._find_and_load(name)
Traceback (most recent call last):
File "C:\Python_3.9\lib\site-packages\py2exe\mf34.py", line 671, in __code__
self.__code_object__ = compile(source, __file__, "exec",
File "_pydev_imps\_pydev_pkgutil_old.pyc", line 543
except IOError, msg:
^
SyntaxError: invalid syntax
Falling back to loader to get code for module _pydev_imps._pydev_pkgutil_old
Traceback (most recent call last):
File "C:\Python_3.9\lib\site-packages\py2exe\mf34.py", line 675, in __code__
raise RuntimeError("compiling %r" % self) from None
RuntimeError: compiling Module(_pydev_imps._pydev_pkgutil_old, 'C:\\Python_3.9\\lib\\site-packages\\debugpy\\_vendored\\pydevd\\_pydev_imps\\_pydev_pkgutil_old.py')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "E:\Python\Programs\Word_Document_Creator\setup.py", line 5, in <module>
setup(
File "C:\Python_3.9\lib\distutils\core.py", line 148, in setup
dist.run_commands()
File "C:\Python_3.9\lib\distutils\dist.py", line 966, in run_commands
self.run_command(cmd)
End:
File "<frozen importlib._bootstrap_external>", line 923, in get_code
File "<frozen importlib._bootstrap_external>", line 853, in source_to_code
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "C:\Python_3.9\lib\site-packages\debugpy\_vendored\pydevd\_pydev_imps\_pydev_pkgutil_old.py", line 543
except IOError, msg:
^
SyntaxError: invalid syntax

Pyinstaller speedtest convert to exe error; no module name "__builtin__"

I am trying to convert a program to an exe using pyinstaller. The program performs a hardware assessment of a user's computer to include running an internet speed test utilizing speedtest-cli. The program runs fine until I compile it at which point I receive the following error:
Traceback (most recent call last): File "speedtest.py", line 156, in <module> ModuleNotFoundError: No module named '__builtin__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "hw-assesment-tool.py", line 9, in <module> File "<frozen importlib._bootstrap>", line 1007, in
_find_and_load File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 680, in _load_unlocked File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module File "speedtest.py", line 179, in <module> File "speedtest.py", line 166, in __init__ AttributeError: 'NullWriter' object has no attribute 'fileno'
I have tried upgrading and re-installing speedtest-cli and it has not corrected the issue.
P.S. Here is a screenshot of the error:
Compiled Python EXE error originating in Speedtest-cli
First you need to go to C:\Users\user\AppData\Local\Programs\Python\Python38\Lib\site-packages. Then find the speedtest.py and open it with Notepad or any other Text Editor.
Now you need to edit these lines of code
Line 156: to
import builtins
Line 158 : to
import builtins
Line 199: to
del builtins
To confirm, you can find for __builtin__ and replace it with builtins
__builtin__ was changed to builtins in Python 3. I pulled the speedtest-cli code from the repo and edited out the Python2 functionality and it worked fine.
I met same issue before, you need to modify the spec file: hiddenimports=['speedtest'], and build exe via spec file(type command: pyinstaller -F main.spec), It's work!

pyttsx3 initialization error, can't use pyttsx3

I was having some problems when using the .getproperty('voices') attribute in pyttsx3. So I decided to uninstall it and then re-install it using PIP to see if that may fix the problem.
Previous link to when I had the .getproperty('voices') error: Why do I get an error with pyttsx3 when I try to access the properties of the voice?
Now, when I installed it again using PIP, I can't even initialize it
import pyttsx3
engine = pyttsx3.init()
It gives this error:
Traceback (most recent call last):
File "C:\Users\saife\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyttsx3\__init__.py", line 20, in init
eng = _activeEngines[driverName]
File "C:\Users\saife\AppData\Local\Programs\Python\Python38-32\lib\weakref.py", line 131, in __getitem__
o = self.data[key]()
KeyError: None
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\saife\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyttsx3\drivers\sapi5.py", line 3, in <module>
from comtypes.gen import SpeechLib # comtypes
ImportError: cannot import name 'SpeechLib' from 'comtypes.gen' (C:\Users\saife\AppData\Local\Programs\Python\Python38-32\lib\site-packages\comtypes\gen\__init__.py)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\saife\AppData\Local\Programs\Python\Python38-32\lib\ctypes\__init__.py", line 123, in WINFUNCTYPE
return _win_functype_cache[(restype, argtypes, flags)]
KeyError: (<class 'ctypes.HRESULT'>, (<class 'comtypes.automation.tagVARIANT'>, <class 'comtypes.LP_POINTER(ISpeechRecoGrammar)'>), 0)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
engine = pyttsx3.init()
File "C:\Users\saife\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyttsx3\__init__.py", line 22, in init
eng = Engine(driverName, debug)
File "C:\Users\saife\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyttsx3\engine.py", line 30, in __init__
self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug)
File "C:\Users\saife\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyttsx3\driver.py", line 50, in __init__
self._module = importlib.import_module(name)
File "C:\Users\saife\AppData\Local\Programs\Python\Python38-32\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\saife\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyttsx3\drivers\sapi5.py", line 6, in <module>
engine = comtypes.client.CreateObject("SAPI.SpVoice")
File "C:\Users\saife\AppData\Local\Programs\Python\Python38-32\lib\site-packages\comtypes\client\__init__.py", line 250, in CreateObject
return _manage(obj, clsid, interface=interface)
File "C:\Users\saife\AppData\Local\Programs\Python\Python38-32\lib\site-packages\comtypes\client\__init__.py", line 188, in _manage
obj = GetBestInterface(obj)
File "C:\Users\saife\AppData\Local\Programs\Python\Python38-32\lib\site-packages\comtypes\client\__init__.py", line 110, in GetBestInterface
mod = GetModule(tlib)
File "C:\Users\saife\AppData\Local\Programs\Python\Python38-32\lib\site-packages\comtypes\client\_generate.py", line 110, in GetModule
mod = _CreateWrapper(tlib, pathname)
File "C:\Users\saife\AppData\Local\Programs\Python\Python38-32\lib\site-packages\comtypes\client\_generate.py", line 184, in _CreateWrapper
mod = _my_import(fullname)
File "C:\Users\saife\AppData\Local\Programs\Python\Python38-32\lib\site-packages\comtypes\client\_generate.py", line 24, in _my_import
return __import__(fullname, globals(), locals(), ['DUMMY'])
File "C:\Users\saife\AppData\Local\Programs\Python\Python38-32\lib\site-packages\comtypes\gen\_C866CA3A_32F7_11D2_9602_00C04F8EE628_0_5_4.py", line 455, in <module>
ISpeechRecoContext._methods_ = [
File "C:\Users\saife\AppData\Local\Programs\Python\Python38-32\lib\site-packages\comtypes\__init__.py", line 329, in __setattr__
self._make_methods(value)
File "C:\Users\saife\AppData\Local\Programs\Python\Python38-32\lib\site-packages\comtypes\__init__.py", line 698, in _make_methods
prototype = WINFUNCTYPE(restype, *argtypes)
File "C:\Users\saife\AppData\Local\Programs\Python\Python38-32\lib\ctypes\__init__.py", line 125, in WINFUNCTYPE
class WinFunctionType(_CFuncPtr):
TypeError: item 1 in _argtypes_ passes a union by value, which is unsupported.
Open command prompt
Write:
pip uninstall pyttsx3
Then:
pip install pyttsx3==2.71
And your problem will solve.
If you are using Linux install libespeak1 using
sudo apt install libespeak1 if using debian based distros.
Try instead this:
engine= pyttsx3.init('dummy')
I hope you are trying to install pyttsx3 from https://pypi.org/project/pyttsx3/.
were you able to execute pip install pyttsx3 with out any issues?
Are you trying to run the above code from jupyter?
if yes then, please don't run the complete code at a time i.e the below code
import pyttsx3
engine = pyttsx3.init()
engine.say("I will speak this text")
engine.runAndWait()
first run import pyttsx3 alone and thern try to put up your code one by one.
I have faced similar issue earlier many times while running it on diferent systems.
I don't know why this issue shows up but the above steps will surely do resolve the issue.
I don't know if you managed to work around this issue, but I had the same thing this morning and fixed it by downgrading the version of pyttsx3 from the latest to version 2.71.
There seems to be some missing commits with the latest version that haven't been resolved (if you read the thread this post is from)
You need to provide a driver key argument to init. 'dummy' or 'sapi5' would work.
engine = pyttsx3.init('dummy')
or
engine = pyttsx3.init('sapi5')
I was facing same problem in my project but simply changing the python version from 3.8 to 3.6 works for me.

Python EEL and PyInstaller Issue

I've recently created a program using eel, in this program I use the module moviepy to split the audio and the video of a file.
But when I try to put the whole project into an .exe file with PyInstaller, the app won't work.
The command I used is: python -m eel main.py web
I tried compiling the program without moviepy (I simply removed the import from main.py) and it perfectly worked.
So the problem is clearly moviepy which is not imported from PyInstaller.
Now I'm asking if there's any way to add moviepy dependencies using PyInstaller?
PS: Starting the source file works perfectly, it doesn't start only after compiled with PyInstaller
EDIT:
When I start the compiled .exe file with cmd, it gives me this error:
Traceback (most recent call last):
File "main.py", line 4, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "C:\Users\Lucad\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\moviepy\editor.py", line 84, in <module>
File "<string>", line 1, in <module>
AttributeError: module 'moviepy.audio.fx.all' has no attribute 'audio_fadein'
[8740] Failed to execute script main
EDIT 2: FOUND A SOLUTION!
I read a thread where people say that moviepy has an error importing some modules, so all I had to do was modifying a file.
This is the link of the guide I followed: https://github.com/Zulko/moviepy/issues/591
Thanks to everybody answering me!
Did you try add moviepy explicitly to the path?
pyinstaller -y -D --name Whatever --clean --distpath="." -p "c:\Python37\Lib\site-packages\moviepy" yourpyfile.pyw

Categories