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

2

1

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)
Expansive answered 1/11, 2021 at 16:34 Comment(0)
D
4

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.

Detective answered 15/11, 2021 at 22:13 Comment(0)
S
0

I had this issue when i tried to build with pyinstaller==5.13.2 , I got the same error Failed to execute script pyi_rth_win32comgenpy then i installed pyinstaller==5.12.0, it worked for me

Scrapbook answered 14/9, 2023 at 6:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.