Executables made with pyinstaller not working - python

I've been teaching myself Python3/API/PyQt5 and OOP for the last few weeks and I have finally made an app that works, yay!
I've been working on Ubuntu 19 and the app works fine when I run it from the terminal. The problem is trying to compile it (correct term?) to make it an executable for Linux and Windows.
I used pyinstaller app.py --onefile -w --icon="app.ico" to make the files and I get no errors that I can see.
I used Windows 10 to compire the exe.
Here is the problem: On Linux I get a "shared library" file that doesn't execute and on windows the exe is just broken. All I get is an error saying "this program cannot run on your system", or something similar to that.
This is a very simple, 1 script app with a PyQt5 GUI. I'm pretty new to all of this and this has by far been the hardest part. I've been stuck on this for days and I can't get it to work.
I can give you guys the app code if it will help, I just don't think I have enough rep to post links so it might have to be a code block.
I will be grateful for any help you guys can give as I'm out of ideas.
Cheers!
Editing to add: windows 8.1 gives the error message "Error -3 from inflate: invalid block lengths" when the app.exe is run

After a lot more tinkering I finally have a working exe.
I used -D to make a directory rather than the --onefile as that doesn't work. It makes an exe that doesn't work. Windows complains that it can't unpack the app.
The exe that is inside the dist directory works on Windows 8.1 and Windows 10. Didn't test on Win 7.
I also included the png and ico files and used a clean env with python 3.8 instead of my working env with 3.7 installed. I'm not sure if any of these things made a difference but if anyone else is looking for an answer, this might help.
My final command:
pyinstaller -y -D -w -i "FULL PATH TO ICON .ico" --add-data "FULL PATH TO IMAGE .png";"." "FULL PATH TO PY FILE /app.py"

To make it work on Unix systems you will probably have to use dos2unix "appname" in Terminal, since windows adds some weird endings to any files containing text. That's how it works for me

Related

pyinstaller seems to need different paths under Microsoft Windows for compiling

in short: if I start my script in Atom I need to specify different paths than when I want to compile it with pyinstaller.
This is quite troublesome, I think I make a mistake that could be avoided. I tried around and googled, but no success so far. Maybe you can assist me here :)
Compiling:
pyinstaller Testscript.py --onefile --noconsole --collect-data selenium
Different Codes are explained with Comments behind
#window.iconbitmap(r'.\test.ico') # Used for creating exe
window.iconbitmap(r'C:\Users\XXX\Desktop\Bot\test.ico') # Used for running in Atom
#s = Service(r'.\chromedriver.exe') # needed for creating exe file!
s = Service(r'C:\Users\XXX\Desktop\Bot\chromedriver.exe') # Used for running in Atom
I hope you could follow me so far.
If I use the following Compiling code with the following lines in Atom, then it's compiling, but not working.
window.iconbitmap(r'.\test.ico') # Used for creating exe
s = Service(r'.\chromedriver.exe') # needed for creating exe file!
pyinstaller Testscript.py --onefile --noconsole --collect-data selenium --add-binary ".\chromedriver.exe;.\driver" --icon=".\test.ico"
The Chromedriver cannot be found and the icon cannot be found too.
So I am actually asking for 2 solutions at once:
Solution: How to put the path in Atom correctly, so I can run the script in Atom and compile it at the same time without changing it?
Solution: How can I use the pyinstaller properly to include chromedriver and the icon correctly in the compiled .exe file and it is working properly? I guess the path in Atom needs correction too.
I am at windows using Atom as my editor and pyinstaller with CMD for compiling. All solutions I found so far were for Linux :(
Please help. Thanks :)

Pyinstaller "Failed to execute script app" error is showing when I run a pygame code exe [duplicate]

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.

Pyinstaller exectuable using data from other folders?

I've written a program that generates a random name and displays a random image along with it using Python and tkinter. However, I want the user to be able to add and remove pictures as well as edit the names of students. That being said, I don't want to package these inside the executable where it can't be changed by the user.
To use PyInstaller, I go into the command prompt and navigate to the working directory and type:
pyi-makespec --windowed --onefile --icon=Assets\\icon.ico random_student.py
Then, in the spec file I change datas to:
datas=[('Assets\\icon.ico', 'Assets')],
Then, I run
pyinstaller random_student.spec
The program runs just fine using PyCharm. And I've done this exact same method on a couple of other .py files an it works. However, they don't need to pull images/text into their programs. This will create an executable, but I can't run it. It gives me a Fatal Error "Failed to execute script random_student". I've tried placing the executable in the working directory and in the pictures folder, but neither work.
I'm currently using Windows 10 64-bit and Python 3.6.6
I'd appreciate any kind of help I can get with this!
SOLUTION: I removed the --windowed option so I could actually read the error. Then realized I didn't have Pillow installed so it was unable to be packaged. Thank you for the help.

pyinstaller exe without any dependencies?

So I'm using pyinstaller with python27, and my exe works great so long as it's in the same directory as the build folder. I need it to be a completely standalone exe, without any dependencies, is there a way to bundle the important things from the build folder into one file? Neither -F nor --onefile seems to do this.
Edit: as I explain in my answer below, I thought pyinstaller was the problem because the exe would only run in the dist folder, so I assumed it had dependencies there, but in reality, it was running and then instantly crashing due to a bug that only triggered when the exe was on the desktop.
I figured out that the reason it wasn't working had nothing to do with pyinstaller or dlls. The exe was opening, and and trying to input powershell commands via python like it was supposed to. Unfortunately I had a line of code that said this:
subprocess.check_output('schtasks /create /sc minute /mo ' + str(time) + ' /tn "test_process_to_run_every_'+str(time)+'_min" /tr //'+sys.argv[0],shell=True)
#set this exe to run every X minutes in windows scheduled tasks
the problem was that sys.argv[0] changed when I put the exe on the desktop, and ended up being a path that looked like C://Users/John Smith/Desktop. The space in between John and Smith made powershell mad and crashed the program, so I escaped it using this line of code:
path = sys.argv[0].replace(" ","^")
and then I replaced sys.argv[0] with my new path variable. Hope this helps anyone in the future trying to do the same thing.
after pyinstaller has converted your script into .exe, than you need to add the executable to path, otherwise you have to open the command line in the directory that the file is in. pyinstaller just puts your script and py interpretor into a single file. same goes for linux.
for dependency side, look here.
there are other options you can try to bbFreeze, py2exe, cx_Freeze
to use pyinstaller in a simple way:
pyinstaller --onefile your_file.py
now you should see couple of files build, dist(exe in here).
NOTE: that --onefile flag doesn't necessarily get rid of the need for it to have link with certain libraries, it will still need those in order to run.
prepare for distribution, first need to get a spec file:
to get a spec file:
pyinstaller --noconsole your_file.py
than you can get the exe file for distribution like so:
pyinstaller your_file.spec
for more info and tutorial look here
see if nuitka works for you, it might sound scary but it is not. it compiles your code to executable binary format. Be aware that under the hood first it converts to c++ API calls.
if you dont like that for closed source program use Cython, and for no dependency use py2exe

Pyinstaller-compiled exe can't find file with absolute path

I compiled my Python GUI with Pyinstaller on Windows 10 but it seems like it cannot find my other script even though I provided the hard-coded absolute path to it (with r'"C:\Program Files...script path..."'). I even tried os.isfile (script path) but it returns False. The python script was compiled with pyinstaller --onefile --windowed --icon=iconimage.ico myscript.py from the command prompt. I use this same command on Ubuntu and the binary works just fine. I read something about Pyinstaller creating a temporary directory which I found, but I don't think it matters where it's running from as long as I give it the full path, so I'm thinking maybe I need more options when compiling? The GUI opens just fine. It's when it needs to call the script that it doesn't do anything. There are no errors when I run it from the command prompt. Please help!
Solved by adding --onedir which will put everything needed to run the program in one directory in the dist folder.

Categories