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

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

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 wont 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 didnt 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 that have dependencies with other files. After I build that app with py installer using this command:

pyinstaller --onefile --windowed

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 and the dependency files, and I got no error after run the main.exe.

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

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:


Then use this command:

pyinstaller --onefile --windowed --add-data todo.ico;.

For more information visit this documentation page.

Leave a Reply

Your email address will not be published.