Windows- Pyinstaller Error "failed to execute script " When App Clicked - python

I am having a tough time overcoming this error, I have searched everywhere for that error message and nothing seems relevant to my situation:
"failed to execute script new-app"
new-app is my python GUI program. When I run pyinstaller using this command:
pyinstaller.exe --onedir --hidden-import FileDialog --windowed --noupx new-app.py
It does work smoothly. In addition, when I execute the command line to run the gui program, it works perfectly and the GUI is generated using this command:
.\dist\new-app\new-app.exe
But when I go to that file hopefully to be able to click the app to get the GUI, it gives me the error said above. Why is that?
I am using python2.7 and the OS is Windows 7 Enterprise.
Any inputs will be appreciated and thanks a lot in advance.

Well I guess I have found the solution for my own question, here is how I did it:
Eventhough I was being able to successfully run the program using normal python command as well as successfully run pyinstaller and be able to execute the app "new_app.exe" using the command line mentioned in the question which in both cases display the GUI with no problem at all. However, only when I click the application it won't allow to display the GUI and no error is generated.
So, What I did is I added an extra parameter --debug in the pyinstaller command and removing the --windowed parameter so that I can see what is actually happening when the app is clicked and I found out there was an error which made a lot of sense when I trace it, it basically complained that "some_image.jpg" no such file or directory.
The reason why it complains and didn't complain when I ran the script from the first place or even using the command line "./" is because the file image existed in the same path as the script located but when pyinstaller created "dist" directory which has the app product it makes a perfect sense that the image file is not there and so I basically moved it to that dist directory where the clickable app is there!
So The Simple answer is to place all the media files or folders which were used by code in the directory where exe file is there.
Second method is to add "--add-data <path to file/folder>"(this can be used multiple times to add different files) option in pyinstaller command this will automatically put the given file or folder into the exe folder.

In my case i have a main.py that have dependencies with other files. After I build that app with py installer using this command:
pyinstaller --onefile --windowed main.py
I got the main.exe inside dist folder. I double clicked on this file, and I raised the error mentioned above.
To fix this, I just copy the main.exe from dist directory to previous directory, which is the root directory of my main.py and the dependency files, and I got no error after run the main.exe.

Add this function at the beginning of your script :
import sys, os
def resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
Refer to your data files by calling the function resource_path(), like this:
resource_path('myimage.gif')
Then use this command:
pyinstaller --onefile --windowed --add-data todo.ico;. script.py
For more information visit this documentation page.

In case anyone doesn't get results from the other answers, I fixed a similar problem by:
adding --hidden-import flags as needed for any missing modules
cleaning up the associated folders and spec files:
rmdir /s /q dist
rmdir /s /q build
del /s /q my_service.spec
Running the commands for installation as Administrator

I was getting this error for a different reason than those listed here, and could not find the solution easily, so I figured I would post here.
Hopefully this is helpful to someone.
My issue was with referencing files in the program. It was not able to find the file listed, because when I was coding it I had the file I wanted to reference in the top level directory and just called
"my_file.png"
when I was calling the files.
pyinstaller did not like this, because even when I was running it from the same folder, it was expecting a full path:
"C:\Files\my_file.png"
Once I changed all of my paths, to the full version of their path, it fixed this issue.

I got the same error and figured out that i wrote my script using Anaconda but pyinstaller tries to pack script on pure python. So, modules not exist in pythons library folder cause this problem.

That error is due to missing of modules in pyinstaller. You can find the missing modules by running script in executable command line, i.e., by removing '-w' from the command. Once you created the command line executable file then in command line it will show the missing modules. By finding those missing modules you can add this to your command :
" --hidden-import = missingmodule "
I solved my problem through this.

I had a similar problem, this was due to the fact that I am using anaconda and not installing the dependencies in pip but in anaconda. What helped me was to install the dependencies in pip.

I found a similar issue but none of the answers up above helped. I found a solution to my problem activating the base environment. Trying once more what I was doing without base I got my GUI.exe executed.
As stated by #Shyrtle, given that once solved my initial problem I wanted to add a background image, I had to pass the entire path of the image even if the file.py and the image itself were in the same directory.

In my case (level noob) I forgot to install library "matplotlib". Program worked in Pycharm, but not when I tried open from terminal. After installed library in Main directory all was ok.

Related

pyinstaller to creating wrong 1 _MEIxxxx folder, but trying to open another

If this is happening to you, the error (In this case) is a broken installation of pyinstaller or python, remove it from the computer and do a fresh reinstallation.
When trying to create a exe with pyinstaller, it works fine and the bundled .dll file is included an it unpacks the _MEI folder with the necesarry files the correct places. BUT i creates one called _MEIxxx but tries to open a _MEIxxY which does not exist (yes, both changes name everytime it is launched)
I cannot see anywhere you can manually set the name of the _MEI folder which would make it much easier.
The cmd command i am running is:
pyinstaller script.py --add-data "PATH TO DLL\python39.dll;test" -F --runtime-tmpdir .
reproducible problem:
creating a fresh .py project with python 3.9(i use pycharm)
include code of:
print("HI")
then in cmd use:
pyinstaller main.py -F (we want it to be a onefile exe)
Then the .exe file is copied to another pc
Here we run it with CMD to see the error output.
It returns the error:
Error loading Python DLL: "path to local\Temp\_MEIXXXX\python39.dll
the error (In this case) is a broken installation of pyinstaller or python, remove it from the computer and do a fresh reinstallation.

missing module aspose in file exe

I wrote a program which combines two or more PDFs in one and converts PDFs in Jpeg or PNG files.
I tried to create the .exe file but the "aspose" module was not found.
I used these command (I found a sample in this question with the same problem Problem using aspose-words after exe generation by auto-py-exe:
myDirectory CombinaPDF_004.py --onefile --collect-binaries "aspose" --collect-submodules "aspose"
What else could I do?
Example of how to create one-file bundled executable using PyInstaller (https://pyinstaller.org):
pyinstaller --noconfirm --onefile --console --collect-binaries "aspose" --collect-submodules "aspose" path/to/script
Its works fine for me.
'Digital.qubit' answer didn't work for me as it missed a step. My aspose modules being imported in my script are aspose.slides and aspose.pydrawing
The steps i took include:
Open command prompt
change directory to where your python script is located using the following command
cd path/to/script
Enter in the command
pyinstaller --noconsole --collect-binaries "aspose" --collect-submodules
"aspose.slides" script_name.py
To check if you need to include any submodules, check your imports in your script file, if your aspose import is it's own folder separate to aspose found in site packages (Mine is located here 'C:\Users\User\AppData\Roaming\Python\Python37\site-packages')
Then include it in the submodules quotations.
FOR EXAMPLE aspose.pydrawing is included in the aspose folder in site-packages so i didn't need to add it as a sub-module command as it is already included when pyinstaller collects the aspose-binaries through the '--collect-binaries "aspose"' command.
Hit enter on your command prompt to create the exe file.
After you have done this, navigate to your pythons site packages folder again usually located here ^
'C:\Users\User\AppData\Roaming\Python\Python37\site-packages'
and copy the aspose and aspose submodule folder, in my case it was "Apose.Slides-22.8.0.dist-info", into your 'dist/application_folder/' created when pyinstaller is finished. When it asks if you want to replace the files or skip, select Skip.
Launch the application and your ready to go.
Not sure if the op managed to get his working but hopefully this helps other people that have run into this issue

Getting folder path to script errors after compiling with pyinstaller

I am working on a small gui using Tkinter when I set the path to the icon in the script it works fine but when I run it as a exe file it says it cant be found and the path it displays is AppData\Local\Temp. What am I doing wrong?
root.iconbitmap(os.path.dirname(os.path.realpath(__file__))+"\\Icon.ico")
for pyinstaller I am using this line:
pyinstaller "Filename" --onefile
I have ran into this problem before. Don't use os.path.dirname(os.path.realpath(__file__)) to get the path the file is currently running in, but rather use sys.argv[0].
See this answer for further explanation.

How do I include poppler to pyinstaller generated exe when using pdf2image?

I made a simple script that converts pdfs inside the current directory to images. I want to make it into a standalone .exe file so that someone who doesn't have python installed on his PC can use it.
The problem is that pyinstaller fails to include poppler to the exe file so pdf2image does not run correctly and the built exe fails. Here's the error message:
pdf2image.exceptions.PDFInfoNotInstalledError: Unable to get page
count. Is poppler installed and in PATH? [32024] Failed to execute
script bulk_pdf2img
I'm currently working on conda environment that has pyinstaller and pdf2image and poppler installed from conda install command. It works just fine when I execute the python script from the prompt but when the script is converted to exe, it raises the above error.
I tried the following approaches:
1. Add --add-data option
I tried to add poppler data by doing this.
$ pyinstaller --onedir --add-data="C:/Users/myusername/anaconda3/pkgs/poppler-0.89.0-h20e2fe3_4/Library/include/poppler/*;./poppler" bulk_pdf2img.py
Doesn't work.
2. Add additional-hooks-dir option
I added projectdirectory/hooks/hook-pdf2image.py that has
from PyInstaller.utils.hooks import collect_all
data, binaries, hiddenimports = collect_all('pdf2image')
inside and ran
$ pyinstaller --onefile --additional-hooks-dir=hooks bulk_pdf2img.py
Also doesn't work.
I googled almost every Stackoverflow question that is apparently having the exact same problem as I am but couldn't find any valid solution. What should I do now?
From the docs
You will then have to add the bin/ folder to PATH or use poppler_path = r"C:\path\to\poppler-xx\bin" as an argument in convert_from_path.
Now, if you are using the --onefile option of pyinstaller it will unpack all the files into a temporary folder during the execution, you might want to look into this answer and the related post, to get the path right.
You can do any of the following
Execute this at the beginning to add the bin folder of poppler to PATH.
os.environ["PATH"]+=os.pathsep+os.path.join('path/to/poppler','bin')
pdf2image.convert_from_path('path/to/pdf',poppler_path=r"path\to\poppler\bin")

Python file converrted to .exe says has files and modules

I have used several tools (pyinstaller, cx_Freeze, py2exe) to convert my .py file to .exe file. But with each one of these, the resultant .exe file closes as soon as it runs. Here is a screenshot of the exe file created form pyinstaller.
I have looked at several answers on SO and other platforms but haven't been able to find a solution. Any help would be greatly appreciated.
My apologies for not being able to type these traccebacks out here as I had to take a quick screenshot as the window closed in a second.
The .py file runs fine on its own; there's no issue with the code.
I had this problem about a year ago with pyinstaller and this 2 methods helped me:
Adding import pkg_resources.py2_warn to my script
Using --onedir instead of --onefile
You can also do it using cmd:
pip install pyinstaller
cd followed by the location where your Python script is stored
so an example is:
cd C:\Users\Ron\Desktop\MyPython
then:
pyinstaller --onefile pythonScriptName.py
instead of pythonscriptname.py, put in your python file name.
When you do this there should be a new file called dist in your directory. Wait for a few mins and there should be an .exe file in there.
Here's a temporay solution: I removed all imports related to the googleapiclient for google docs. The .py file gets compiled properly after that.
More Context: There seems to be some issue in this particular library library that causes issues. I have tested the given solutions, they don't seem to work for me. I'll update this answer if I find a "permanent" solution.

Categories

Resources