Python Pyinstaller build fails using win32com package [duplicate] - python

I am trying to convert my .py file to an .exe file and I have tried all methods (auto-py-to-exe, pyinstaller, cz_freeze) and it does create the exe file but it always gives an error or the window opens and closes as soon as I double click the file.
It is a SpeechRecognition AI project coded in python. And it works just fine in the IDLE but once I create the .exe and try to run it the window pops up and shut down immediately after. (I use the cx_freeze and setup.py method for this)
If I try to convert .py to .exe using pyinstaller it gives me several different kinds of error messages.
As a .py file it works just fine but it doesn't work as an exe.
This is the error I get when using pyinstaller or auto-py-to-exe: Failed to execute script 'pyi_rth_win32comgenpy' due to unhandled exception: Module 'pythoncom' isn't in frozen sys.path
Module 'pythoncom' isn't in frozen sys.path
I tried several things but nothing seems to work. I was previously using Python3.10 so I uninstalled it and downgraded to Python3.8 and reinstalled all the modules so technically it should work. I tried to create .exe files of another project and it worked just fine.
Another issue I come across is ModuleNotFoundError: No module named 'pyttsx3.drivers' I compiled the .exe using cx_freeze and it did create an .exe but it gives me this error.
Could someone please help me out with this?
(PS: This is the list of imports I am using for this project:
screenshot of all imports
import speech_recognition as sr
import pyttsx3
import datetime
import wikipedia
import wikipediaapi
import webbrowser
import os
import time
import subprocess
import wolframalpha
import json
import requests
from newsapi import NewsApiClient)

I just solved this for my project, which is using none of the direct imports your project is, but it appears the same low level library is causing an issue.
In my case, the single import is:
from pywinauto.application import Application
The solution is to tell PyInstaller to bundle the missing .DLL, which in this case is pythoncomxx.dll where xx is your python version. 39 in my case.
Here is the call that eventually worked for me.
pyinstaller --onefile --add-binary ".venv/Lib/site-packages/pywin32_system32/pythoncom39.dll;." autoTest.py
More generally: pyinstaller --onefile --add-binary "[path to dll];." file.py
Note, I'm on Windows. If you are on mac/linux, the ; character in the --add-binary argument would be :. See note in the documentation here
Discussion
This clicked for me when I used ProcMon to profile the file access of my initial version that was failing. It was looking for this .DLL in a bunch of different folders, then quitting.
The answers to this question were helpful. But I didn't want to settle for copying the DLL to output manually. And consequently, having to use a directory output versus a single executable file.
Answers on this question also gave the hint about using the --add-binary flag, but ultimately I had to provide the specific path to the missing DLL instead of just referencing by name as these answers showed. It probably works to specify the DLL by name if it is accessible on your PATH.
How to locate this DLL in the first place? Do a search on your site-packages folder.

Related

Unable to convert .py file to .exe using auto-py-to-exe due to import error

I attempted to convert and package my python file designed to send out emails to a .exe so a coworker could use it without knowing python, but I've received this error first when attempting to directly use pyinstaller, and later when discovering auto-py-to-exe.
The error is as follows:
ImportError: cannot import name 'can_import_module' from 'PyInstaller.utils.hooks' (c:\users\alex gille\anaconda3\lib\site-packages\PyInstaller\utils\hooks\__init__.py)
I've attempted to update and reinstall pyinstaller in the command prompt, but this hasn't fixed the problem. I'm doing this within Anaconda as well for added context. The script is based around tkinter and yagmail. Happy to provide the code of the script this is attempting to convert if need-be.

Failed to execute script 'pyi_rth_win32comgenpy' due to unhandled exception: Module 'pythoncom' isn't in frozen sys.path

I am trying to convert my .py file to an .exe file and I have tried all methods (auto-py-to-exe, pyinstaller, cz_freeze) and it does create the exe file but it always gives an error or the window opens and closes as soon as I double click the file.
It is a SpeechRecognition AI project coded in python. And it works just fine in the IDLE but once I create the .exe and try to run it the window pops up and shut down immediately after. (I use the cx_freeze and setup.py method for this)
If I try to convert .py to .exe using pyinstaller it gives me several different kinds of error messages.
As a .py file it works just fine but it doesn't work as an exe.
This is the error I get when using pyinstaller or auto-py-to-exe: Failed to execute script 'pyi_rth_win32comgenpy' due to unhandled exception: Module 'pythoncom' isn't in frozen sys.path
Module 'pythoncom' isn't in frozen sys.path
I tried several things but nothing seems to work. I was previously using Python3.10 so I uninstalled it and downgraded to Python3.8 and reinstalled all the modules so technically it should work. I tried to create .exe files of another project and it worked just fine.
Another issue I come across is ModuleNotFoundError: No module named 'pyttsx3.drivers' I compiled the .exe using cx_freeze and it did create an .exe but it gives me this error.
Could someone please help me out with this?
(PS: This is the list of imports I am using for this project:
screenshot of all imports
import speech_recognition as sr
import pyttsx3
import datetime
import wikipedia
import wikipediaapi
import webbrowser
import os
import time
import subprocess
import wolframalpha
import json
import requests
from newsapi import NewsApiClient)
I just solved this for my project, which is using none of the direct imports your project is, but it appears the same low level library is causing an issue.
In my case, the single import is:
from pywinauto.application import Application
The solution is to tell PyInstaller to bundle the missing .DLL, which in this case is pythoncomxx.dll where xx is your python version. 39 in my case.
Here is the call that eventually worked for me.
pyinstaller --onefile --add-binary ".venv/Lib/site-packages/pywin32_system32/pythoncom39.dll;." autoTest.py
More generally: pyinstaller --onefile --add-binary "[path to dll];." file.py
Note, I'm on Windows. If you are on mac/linux, the ; character in the --add-binary argument would be :. See note in the documentation here
Discussion
This clicked for me when I used ProcMon to profile the file access of my initial version that was failing. It was looking for this .DLL in a bunch of different folders, then quitting.
The answers to this question were helpful. But I didn't want to settle for copying the DLL to output manually. And consequently, having to use a directory output versus a single executable file.
Answers on this question also gave the hint about using the --add-binary flag, but ultimately I had to provide the specific path to the missing DLL instead of just referencing by name as these answers showed. It probably works to specify the DLL by name if it is accessible on your PATH.
How to locate this DLL in the first place? Do a search on your site-packages folder.

pyinstaller importerror when module is in the same directory

Pyinstaller recently stopped importing a module that it previously had no problem with so I set up a simple test case to isolate the problem.
I created a script (pt.py) as follows:
import py_script1
import py_script
print('hello world')
py_script.py and py_script1.py are identical and located in the same directory as pt.py. They both import os, random and math and have some functions in them.
When running pyinstaller with default settings it compiles the .exe file but when I try to run it, I get "ImportError: No module named py_script". Checking the warnings text file shows that it was never compiled into the .exe file.
Changing the order of the imports doesn't help - I get the same error. I have also tried to uninstall and reinstall pyinstaller but that has not helped
How is this even possible - the two imported scripts are identical and are located in the same place!
More importantly how do I fix this problem?
PyInstaller 3.3.1
Python 3.3.5
Windows 10
I solved it. Despite the misleading exception description I isolated the problem as the following line in the imported file:
SB_CONSTANT = 0.00000005670373 #Stefan–Boltzmann constant (W/m2/K4)
For some reason this number broke pyinstaller. I changed it to:
SB_CONSTANT = 5.6704e-8
...and the module imported fine.

import statement is not working when running python script from the command line

I need to run a python script from the command line (OS = Debian wheezy, python -version 3.5).
I used PyCharm (community edition) to write the script and it is working from inside the IDE.
I used sys.path.append command to add the directory containing the package I want, then followed it with this import line:
from package_name,file_name import ClassName
The Error message in the command line:
ImportError: No module named 'package_name'
if you are running any xxx.py file and you face the import error though same script if run by any IDE works fine,its path issue.
What worked fine for me is:
Go to file which shows import module issue and before importing module(for which issue is seen),add the path of module to sys using append.
for example ,I was running the script file from conf path and my script was importing module situated in \scripts\Setup\ so appended the path of module like below.
import sys
import os
conf_path = os.getcwd()
sys.path.append(conf_path)
sys.path.append(conf_path + '\scripts\Setup')
then use import statement of module for which issue was thrown.
I found the answer for my question above, and the problem was much easier than I thought.
Addressing the Problem
having many python packages in different directories
your script needs some/all packages, that are not in the standard lib-directory for your python installation (e.g.:prefix/lib/pythonVersion).
Solution
Short term solution
As long you are using an IDE (e.g. PyCharm), it is sufficient within the code to add:
import sys
sys.path.append("path/to/package")
As soon as you have to run your script from the command line, you will get an ImportError as mentioned in the Question above.
Better solution
Add the directories of your packages and of your python installation to your shell-profile(e.g.: .bashrc) using the command:
export PYTHONPATH=prefix/lib/pythonVersion:/path/to/packages
To get more info about PYTHONPATH, check this link
In this case you will not need to append the path of your packages within your code :)

No module named pyopencl (Py2exe)

I am having some trouble with the PyOpenCL module when trying to make a .exe from Py2Exe.
Py2Exe makes the .exe as it should do (No "ImportError: No module named pyopencl" here), but when I am running the .exe it says no module named pyopencl.
I am trying to make a .exe of the Phoenix Miner.
My setup.py:
from distutils.core import setup
import py2exe, sys, os, pyopencl
sys.argv.append('py2exe')
setup(
options = {'py2exe': {'bundle_files': 1,
"includes":["pyopencl","twisted",
"zope","QueueReader",
"numpy"]}},
console=[{'script' : 'phoenix.py'}],
data_files=["C:\\Users\\Nicklas\\Desktop\\Phoenix-Miner\\kernels\\poclbm\\kernel.cl"],
zipfile = None,
)
I found someone who had the same problem as me http://bytes.com/topic/python/answers/848048-py2exe-module-error but with no solution.
UPDATE:
I found what was causing this error. In pyopencl __init__ there is a function called _find_pyopencl_include_path, it is quite self explanatory what it does. To make a long story short: the imp module fails to find the pyopencl module. To fix this I commented out that line and set pathname to the path to pyopencls include directory. Probably not a good fix. But it was an easy fix.
I found what was causing this error. In pyopencl __init__ there is a function called _find_pyopencl_include_path, it is quite self explanatory what it does. To make a long story short: the imp module fails to find the pyopencl module. To fix this I commented out that line and set pathname to the path to pyopencls include directory. Probably not a good fix. But it was an easy fix.
There is also easiest way to make python script executable by using auto-py-to-exe module, it can be installed using pip install auto-py-to-exe and after installing write auto-py-to-exe in python command line terminal a window will appear, choose the script location and choose OneFile instead of OneDirectory, for reference here I am pasting the auto-py-to-exe window screenshot
After converting 'open output folder' option will appear where you'll get your .exe file of python script. And this .exe file run on any system without using python.

Categories