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
Related
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 am trying to add exactly eight .png files and one .json file to a .app I am trying to create using PyInstaller, or, if I have to, I can use Py2App. The program I am trying to turn to a .app is a tkinter GUI application, if that's relevant in any way.
With PyInstaller I can get the .app to form correctly by doing
pyi-makespec --windowed --icon myicon.icns myapp.py
then adding the files under the datas attribute in the .spec file, and then finally doing
pyinstaller myapp.spec
However, when I run the program its raising a python error saying it can't locate the files, even though I checked inside the .app and they are there. While troubleshooting, I tried doing print(os.getcwd()) and it said it was in /User/Robert, which I presume is the problem (the .app is not located in that directory).
As for using Py2App, I can't even figure out how to properly add the files. I tried
py2applet --make-setup --iconfile myicon.icns myapp.py
and then added all their paths within the generated DATA_FILES list, but I just get an error while it is forming the .app after executing
python3 setup.py py2app
So if anybody knows how I can form a .app file from a tkinter application that makes use of certain files, it would be greatly appreciated.
Thanks in advance.
PS: I am using python 3.5.1 (for both the .py file being converted and the conversion process) and my mac OS X version is 10.11, if that helps in any way.
PPS: I have been able to get this exact same program to work on windows in the form of a .exe (converted using PyInstaller).
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.
I am trying to use PyInstaller for generating an .Exe from a Python 2.7 file. In the CMD window, I run pyinstaller myfile.py.
It creates a build and dist folder both of which have a number of files, including an Application file. When I click both application files, a CMD box pops up and very quickly disappears, despite my file requiring inputs from the user.
What I am missing here? Which file can I distribute to be a usable copy?
By default, PyInstaller generates a one-folder bundle containing an executable, it also creates this executable with a console window for standard input/output. I'm just guessing, but your script don't have a GUI, right?
In any case, the better way to work is creating a one-file bundle:
pyinstaller -F myfile.py
In this way, you only have to execute one file.
if after executing the application, it behaves in the same way, I would say that adding the -d option will help you to find out what is going wrong with your generated executable. Also, running your application from a per-existing CMD window is recommendable since these windows do not close itself after running your application.