Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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.

Explorer sets the working directory to that of the opened target file when double-clicking on the file. But your executable could be run with any working directory, and it should still be able to find all of its data relative to itself. Typically with a frozen program that's os.path.dirname(sys.argv[0]) or os.path.dirname(sys.executable). Preferably use os.path.join to create paths for your data files, but you can also change the working directory via os.chdir. – Eryk Sun Nov 21, 2016 at 12:58 I didn't know about the --debug option. That helped a lot for my issue. All of the pyinstaller options can be found here (pyinstaller.readthedocs.io/en/stable/usage.html). – BigRed118 Mar 20, 2021 at 19:32

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.

@hamedbaziyad just move the exe file from "dist" folder to the parent of the "dist" folder. – Pranit Bankar Oct 25, 2020 at 18:36 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.

  • 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.

    This was exactly the problem I also faced. My pygame module was importing some images and the paths were relative to the current working directory. I just copied all the images to the top level directory where the exe was formed and it solved the issue. – Manu S Pillai Feb 14, 2021 at 19:41

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