How to get exe from Python file; ModuleNotFound error - python

I want to transform a Python script to an .exe to share it with friends. I've tried with auto-py-to-exe, but here is what I get when I open it:
Traceback (most recent call last):
File "main.py", line 1, in <module>
ModuleNotFoundError: No module named 'pygame'
I saw somewhere that I had to indicate in auto-py-to-exe's interface the module name here:
But I still get the same error.

I have experienced this before where I export .py into .exe with --onedir setup. Here are the solutions that I might suggest (and works for me)
1. Export as --onefile
auto-py-to-exe screenshot
Exporting using --onefile creates only a single .exe file instead of having a folder contain hundreds of other supporting files (is that term exist? IDK). but the drawback is the file will be painstakingly slow to load up.
but if you export it as --onefile but still doesn't work,
2. Ensure the pygame is in the same directory as your .exe file
Apparently your .exe app is looking for the pygame module but failed. Try putting it in the same directory as your source code

There are multiple ways to convert a python file(.py) to an executable(.exe) -:
cx_Freeze
pyinstaller
auto-py-to-exe
Also, as mentioned in the comments by #Nanthakumar JJ and by #Amar Haiqal in his answer, make sure the --onefile option is checked while using auto-py-to-exe for conversion. Further, pygame module has to be present within the directory of the main file that is being compiled, as it will look for pygame and raise a ModuleNotFoundError, if the module is not found, being an .exe it will not look for the module on PYTHONPATH.

Related

Python Pyinstaller build fails using win32com package [duplicate]

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.

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 - Python exe when run shows error "Failed to execute script pyi_rth_nltk"

I have developed a simple software in python with GUI. I'm actually working on Natural Language Processing and I've just put the whole NLP process in a GUI.
I tried to convert the whole project to a exe file in python using Pyinstaller. I successfully converted it to an exe file but when I run it , it shows an error message something like this
This is the image of my error
I have already solved it but by using another way of converting py to exe which is the cx_Freeze.
I had the same issue earlier today and finally got it to work using the following software versions:
Python 3.6.8, nltk 3.5 and a dev version of pyinstaller:
pip install https://github.com/pyinstaller/pyinstaller/archive/develop.zip
Additionally, I used scikit-learn version 0.21.1.
Few days back, I had the same problem while compressing to EXE. This Problem generally occurs when PyInstaller failes to find Libraries and Modules to import to the directory. But I overcame this problem and the wise solution yet tedious method to do is mentioned below,
Convert Python Scripts to EXE with console = True in the .spec file or to be simple, do not add --noconsole argument while converting using System Argmuents method.(execute GUI with CMD)
After successfully building the file, go to respective directory (dist folder) and open command prompt
Run the Exe file using Command Prompt.
Find the error message and rectify it correspondingly. For example, consider the following Error Message, vcomp140.dll is missing from \\sklearn\\libs directory.
At the end of this Error Message, you'll find Failed to load dynlib/dll , Therefore, find the file or program which is missing. Say, Here we've .libs\\vcomp140.dll
Find vcomp140.dll using window search bar in your C Drive.
Copy the specific file and paste in the directory(under "dist" folder) where the file is missing. Here, the directory is dist\\PyScriptToEXE\\sklearn\\.libs
MatplotlibDeprecationWarning:
The MATPLOTLIBDATA environment variable was deprecated in Matplotlib 3.1 and will be removed in 3.3.
exec(bytecode, module.__dict__)
Traceback (most recent call last):
...
...
...
py3.7.egg\PyInstaller\loader\pyiboot01_bootstrap.py", line 169, in __init__
__main__.PyInstallerImportError: Failed to load dynlib/dll 'C:\\Users\\MOHAMM~1\\AppData\\Local\\Temp\\_MEI38242\\sklearn\\.libs\\vcomp140.dll'. Most probably this dynlib/dll was not found when the application was frozen.
[13968] Failed to execute script try
Follow the steps again using CMD to eliminate each error.

converting .py to .exe using Python 3.5/3.6?

I created an application in Python 3.6 with a GUI built in PyQt5. Some modules are only available for Python 3.5 and 3.6.
I managed to pack it on Mac using py2app. I am trying to do the same thing on Windows. I tried to use this guide, but I still have issues.
For what I understand I have 3 options:
py2exe
pyinstaller
cx_Freeze
1) py2exe
According to here py2exe is still not available for python3.6, I need to use python3.5 then.
Using 3.5, I get this error and the compilation stops:
error: [Errno 2] No such file or directory: 'C:\\Users\\carlo\\AppData\\Roaming\\Python\\Python35\\site-packages\\py2exe\\run-py3.5-win32.exe'
2) pyinstaller
Running pyinstaller through Python3.5 as:
pyinstaller --onefile APP.py
I get just a bunch of warnings (lib not found) but the compilation gets to the end. The created EXE, though, opens the command prompt and pops up the following error:
Traceback (most recent call last):
File "site-packages\PyInstaller\loader\rthooks\pyi_rth_qt5plugins.py", line 46, in <module>
File "c:\program files (x86)\python35-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 573, in load_module
module = loader.load_module(fullname)
ImportError: DLL load failed: %1 is not a valid Win32 application.
Failed to execute script pyi_rth_qt5plugins
3) cx_Freeze
Using python3.5, it first give the 'TCL_LIBRARY' error. Following the instructions here the compilation goes smoothly. The created application, however, opens up and closes immediately. It simply doesn't work.
Using python3.6, the compilation seems okay. When I try to open the application, this error pops up. I just managed to take a quick screen-shot:
Does anybody have any suggestion? Any alternative I am not considering?
I managed to solve the problem!
I followed the instructions here.
I first ran (with Python3.5):
pyinstaller CodeName.py
Then I modified the .spec file as indicated in the link and the magic happened!

Py2exe, Tkinter, and Setup File Problems?

I just finished creating a python program in 2.7 and I converted it to a .exe with py2exe.
Everything works fine when I run the converted executable file in the folder I placed it in with all of the images in it. After converting the python program to .exe, I proceeded to creating a setup file for it. I added all of the files associated with my project including tkinter in the setup file. I added pretty much everything that let me run the executable.
Once I finished creating the setup file, I opened it. I went through everything and finished installing it on my system and created a shortcut on my Desktop. When I tried to open it, it would not work. Instead of running the program, it tells me to open a log file in its folder in the Program Files. When I open the log file, I noticed an error. How do I fix this?
Error:
Traceback (most recent call last):
File "gui.py", line 10, in <module>
File "Tkinter.pyc", line 1764, in __init__
_tkinter.TclError: Can't find a usable init.tcl in the following directories:
{C:/Program Files (x86)/lib/tcl8.5} {C:/Program Files (x86)/lib/tcl8.5} C:/lib/tcl8.5 {C:/Program Files (x86)/library} C:/library C:/tcl8.5.15/library C:/tcl8.5.15/library
This probably means that Tcl wasn't installed properly.
I found a bug on the virutalenv site which suggested the following https://github.com/pypa/virtualenv/issues/93
I imagine that you are encountering the same issue just without virtualenv
the following set the correct paths which can then be included in the application please find the right path to TCL and TK for your python version
set "TCL_LIBRARY=C:\Python27\tcl\tcl8.5"
set "TK_LIBRARY=C:\Python27\tcl\tk8.5"
restart your cmd or shell
I believe that the TCL location have changed from there default ones.

Categories