Why can't I compile a script that contains pyttsx3? - python-3.x

I have been working using pyttsx3 on win7 64bit python 3.4.3. Everything is good with the .py script I made using pyttsx3 module (works fine alone).
The problem comes when I compile it with pyinstaller. I got an error saying "pyttsx3.drivers not found" and I fixed that by using a .spec file with hiddenimports. Then I got an error that says pywintypes.com_error:(-2147352573,member not found,none,none).
I found on github that someone by the name #natambashat fixed it by commenting out the pyi-rth-win32comgenpy.py runtime hook because that's only needed for pyttsx. But that didnt work for me, I still get the same error. Can you please help me?

You cannot compile a script that contains pyttsx3 with pyinstaller because, pyinstaller like all compilers do not have every single python module. Here is a link that shows you all the compatible packages for pyinstaller.
Another option is py2exe. I am not a Windows user, so I don't know how to use it. But it supposedly supports pyttsx.
Another option though untested by me, is to download the pyttsx source code, put it into your project directory, import it in the code that uses pyttsx, then compile it with pyinstaller using:
pyinstaller --onefile app.py

Eureka!!i fixed it! Am gonna write what i did step by step in case some one come-across to the same problem.
1.i made a hook-pyttsx3.py file and commented out the pyi-rth-win32comgenpy.py file(have a look at this link of github.( instead of commenting out,you can also remove the line "win32:pyi-rth-win32comgenpy.py" from the rthooks.DAT file in ...Lib/sitepackages/pyinstaller/loader/rthooks).
2.go to ...Lib/sitepackages/Win32com/client/dynamic.py and to the _GetDeskInvokeType function.Replace the last line(return invoke_type) with return varkind.
->For some people just step 1 works,but for me i need to apply step2.I found somewhere in sourceforge(i dont remember the link),when compiling a script that involves win32,pywin32 version 221 and 220 gives the pywintypes error i mentioned above.But pywin32 version 219 doesnt give this error(i am using pywin32 v221).And the reason behind that is,in the function i mentioned in step2,return invoke_type is only on version 221 and 220 but the return on v219 is return varkind.I don't know why this differece came if it gives such errors!
EDIT:This link Is better than the above i gave.

Related

import win32api in python 3.7 resulting in dll import error

I am having the error while running jupyter notebook. Error replication:
python --version
Python 3.7.9
python
import win32api
The error was: ImportError: DLL load failed
After doing a some stackoverflow, I got to know that there are 2 dll files missing namely: pythoncom37.dll and pywintypes37.dll
I also got to know that I can run this post command: pywin32_postinstall.py in the Scripts folder. I ran this script. Restarted my pc. I manually download these two dll and copied it to my system32. After manually downloading, I started to have a different type of error: ImportError: DLL load failed: %1 is not a valid Win32 application
I used anaconda and ran my jupyter notebook which was my main aim but can you please make me aware as what is going wrong?
I've run into this recently, but with a different version of the DLLs. What solved it for me was using a different version of pywin32.
My solution (conda env, python 3.8.5):
pip install pywin32==300
or try 225, 227, 228. The latest pywin32 (301 as of this post) seems to be having dll search issues (I wouldn't be surprised if whatever version you were using is also having dll search issues). 301 was released after your issue started, but you may have a similar problem nonetheless.
There is currently an issue on pywin32 DLL loading failing: https://github.com/mhammond/pywin32/issues/1709
Factors involved (in my experience) include your PATH variable (if you're using conda). I haven't tested it myself, but I'd be curious to see if this issue occurs without conda. This issue stops happening for me if the first dlls found are those for 301. In my case, that means putting them in my C:\Windows\System32 folder (yeah I'm on Windows; joy).
So a possible solution #2 would be to run the pywin32 post install script which should be located under your venv/Scripts/pywin32_postinstall.py
To try that solution, open an ADMIN command prompt (very important that it's admin), navigate to your venv, and run:
ppython.exe Scripts\pywin32_postinstall.py --install
You shouldn't HAVE to do this, but if you just need a one-off solution and it works, great!
pip install --upgrade pywin32 ==225 worked for me. Tried version 300 and was unsuccessful.

Recurrent error when using pyinstaller & cx_Freeze

I've seen through the web that .exe taken from python scripts may often run into errors because of some "hooks missing", which happens because pyinstaller wasn't able to track some modules while creating the .exe file. I'm currently using Python IDLE 3.61 and the scripts works fine without any error. The .exe actually runs but, for instance, it simply crushes when I try to plot a table giving the error:
NoModuleFoundError: 'No module=plotly.validators.table found'.
Building the .exe also via cx_Freeze, I came up to the same sort of problem:
Module plotly.validators.table has no Attribute CellsValidators
which confirmed me the problem is caused by issues with plotly.
Uninstall plotly module.
Install older version of plotly module.
Now try building .exe file
(If not working again, try further more old versions of plotly)
I hope it works.

Pyinstaller seems to struggle with formulas package

There is some code I want to turn into an exe. I have used Pyinstaller and I manage getting an exe that runs. The platform is Win10, Python version 3.6, devs with Spyder.
At some point in the code, the following lines pose an issue with the exe (not in script mode):
from formulas import parser as formulasparser
# code including classes
# in a function:
t = 'x<1' # any string that is a 'formula', here provided for the example
parser_ = formulasparser.Parser()
# the exe works so far, I have traced the code and can read the messages in the windows cmd
func = parser_.ast(t)
# nothing happens and no way to get a trace; there is an issue, and I have no clue which one as there
# is no error message.
So, it seems that the ast function won't work for some reason. Can anyone help me? I need to distribute this application and it does not work because of this right now.
Thanks for your help.
Well, someone seems to have faced a somewhat similar issue on the Pyinstaller mailing list.
So: a hook file is actually needed. I created a directory called pyinstallerhooks with a file called hook-formulas.py inside.
The file itself contains:
from PyInstaller.utils.hooks import collect_all
datas, binaries, hiddenimports = collect_all('formulas')
and this is it.
When calling pyinstaller, use the following command:
pyinstaller --additional-hooks-dir pathtoyourdir -F yoursrcipt.py
Pyinstaller fetches the relevant dependencies for the formulas package and it shoudl work.

Can't get .exe to execute on other machine - "No module named serial" error

System: Python 3.7 64x running on Windows 10. I also have Anaconda installed on my machine and I program through VS Code. All modules have been installed, verified installed, and are up-to-date.
I have written a script and compiled it into an .exe using pyinstaller. pyinstaller -wFi pyico.ico endpointapp3.py.The .exe works and runs on my machine but it's not meant for my computer. Previous iterations of this script have been compiled, tested, and worked appropriately on its destination computer, a Windows 7 machine with no python installed.
My latest version now includes pyserial, and works, again, on my machine, but I after compiling and creating an .exe file for the destination machine I encounter this error.
File "endpointapp2.py", line 9 in <module>
ModuleNotFoundError: No module named 'serial'
[3512] Failed to execute script endpointapp2.py
Line 9 is my import serial line
As I mentioned before, this executable runs on my machine, but not on the destination machine it was designed for. I don't want to install python, or python modules on the destination machine because I want the .exe to be all inclusive.
Questions:
Why am I getting this error? What does it mean?
Is there a work around that allows me to compile everything together (using pyinstaller) that will hold all the necessary module
information within the .exe?
Is there something else I should be adding to my original script that will allow me to work through this?
NOT A DUPLICATE - This post is for compiling and running python .exe's on another machine, independent of python, with no modules or python installed.
With some assistance and helpful advice (thanks martineau) I was able to solve my problem. When compiling with pyinstaller, it makes a .spec file that it actually uses to compile to .exe.
Here I found the necessary steps to make sure I was adding the files I needed for pyinstaller.
Here I found out how to properly add the module information into my compiling code.
Once pyinstaller had the correct info in the .spec file I was able to compile and execute on the destination machine as intended.
I solve this issue when i move my code into serial module location.
First find where serial module locate :
>>> import serial
>>> serial.__file__
'C:\\Program Files\\Python310\\lib\\site-packages\\serial\\__init__.py'
mine is above.
Then move your file into :
'C:\\Program Files\\Python310\\lib\\site-packages\\{your_file.py}'
Then run pyinstaller :
pyinstaller --onefile your_file.py --additional-hooks-dir serial\\__init__.py
Run your .exe from dist folder. No longer showing error.
The module is called pyserial, but the library call in the code is import serial. Pyinstaller is unable to complete the build because there truly is no module named serial.
To correct this you need to tell pyinstaller to import the correct package by including it in the .spec file under hiddenimports.
hiddenimports=['pyserial']
This will be true for any case where the package name and the import statement call are not the same.

cx_Freeze not found error-python

Ok, I am using python 3.4.3 and I think I downloaded the right file but when I go to python shell, it says No module named 'cx_Freeze'
I know there are plenty of questions like this but none of them helped. There was one I found using my exact same problem and version but even that did not work. I do not know what to do. I have put the file in the same place, I think anyways, as python is and I tried putting it on my desktop but still does not work. Any ideas?
faced a similar problem (Python 3.4 32-bit, on Windows 7 64-bit). After installation of cx_freeze, three files appeared in c:\Python34\Scripts:
cxfreeze
cxfreeze-postinstall
cxfreeze-quickstart
These files have no file extensions, but appear to be Python scripts. When you run python.exe cxfreeze-postinstall from the command prompt, two batch files are being created in the Python scripts directory:
cxfreeze.bat
cxfreeze-quickstart.bat
From that moment on, you should be able to run cx_freeze.
cx_freeze was installed using the provided win32 installer (cx_Freeze-4.3.3.win32-py3.4.exe). Installing it using pip gave exactly the same result.
Ok, I figured it out. So this is for all the future people have the same problem as I am. First, download pip. Then open a python shell and import pip. This is to make sure the download of pip was successful. Then go to the cx_Freeze website and for python 3.4.3, it will be the last one I think. It will say the version of cx_Freeze and then say the version of python which is 3.4.3 for me. That will download and then go to python shell and import cx_Freeze. It should work. Remember that you have to capitalize the "F" and have the code be exactly like this "cx_Freeze" but without the quotes. That is how I solved this problem with this exact python version.

Resources