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 :)
Related
I've been stuck on the very last step of creating my program.
I am trying to pack it in to an executable file using pyinstaller, but there is this weird bug (?), I've come across.
I tried packaging it using
pyinstaller --icon=64x64.ico --noconsole file.py
Which results into build folder, spec file and dist folder, but the dist folder does not contain exe file. If, however, I remove the --noconsole part, everything works fine, though, with the console window in the background when running. The code does not interact with the console at all, and I even tried using it with the console open, to see it if displays anything, and the results were negative.
Any ideas what to look for?
EDIT:
Managed to solve the problem, but further investigating #VictorS's suggestion with pyw extension. Tried running pyinstaller kalkulators_v2.0.pyw in CMD , which managed to create all the files in dist folder and also surpress the console
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
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.
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
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.