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

Question:

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:

.distnew-appnew-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.

Asked By: aBiologist

||

Answers:

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.

Answered By: aBiologist

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.

Answered By: J-Mourad

In case anyone doesn’t get results from the other answers, I fixed a similar problem by:

  1. adding --hidden-import flags as needed for any missing modules

  2. cleaning up the associated folders and spec files:

rmdir /s /q dist

rmdir /s /q build

del /s /q my_service.spec

  1. Running the commands for installation as Administrator
Answered By: JacobIRR

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.

Answered By: Fatih1923

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.

Answered By: Arshad

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:Filesmy_file.png"

Once I changed all of my paths, to the full version of their path, it fixed this issue.

Answered By: Shyrtle

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.

Answered By: monti

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.

Answered By: Firingam

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.

Answered By: Joey Schuitemaker

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.

Answered By: Kajman