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.
Related
I am very new to programming. I am trying to create a desktop icon that, when clicked on, will run my Python script on Mac OS.
The first line of my code involves input from the user. How can I create a desktop icon of my Python script, that when clicked, allows the user to enter the input in order to cause the Python script to start?
I have tried Pyinstaller; however, when I enter:
pyinstaller "myprogram".py
into the Mac terminal, I receive a "dist" file that includes Python script with an ".exe" file extension. However, when I click on this extension, I receive this response in the terminal window:
logout
Saving session...
...copying shared history...
...saving history...truncating history
files...
...completed.
I would like the Python script to run in order to have a user type their input in order to have the program run. Any help would be greatly appreciated.
Pyinstaller detects the OS and the version you make is only compatible with the system you build it on, they mention that on their page, so it shouldn't be building an .exe; whenever I use pyinstaller on my mac the dist has a .app and a unix executable, you don't have both of those files in there?
also, I just notices your syntax, you should not have the filename in quotations
try:
pyinstaller --clean --windowed --onefile yourapp.py
in your terminal from the directory that has your app. First delete the old folders and .spec it created the first time as well.
Also, I just went through a bundle of issues in a similar vein. If the problem persists after you delete and rerun pyinstaller with proper syntax, check out this page, I have a rather large answer all the way at the bottom, and there are other angles on the same issue there as well.
https://github.com/pyinstaller/pyinstaller/issues/3820
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 have a problem to over come the following error, when i am running my program packed using pyinstallet into standalone executable.
In the program i am using an icon file to set iconbitmap. The file is located in the same folder as the python script.
master = Tk()
master.wm_title("P&P Util ")
master.iconbitmap('logo.ico')
In addition i am adding and icon to the executable in the packing process.
pyinstaller -F -w -i "C:\temp\Util\logo.ico" main.py
Now after some testing i located that when i add file itself to the packed executable, the program is running without any issues. I understand that i need to change the way i am using the file but i don't know how.
Your advice will be very much appreciated.
It looks like it is crashing because you have not added the "logo.ico" file to the package, so when master.iconbitmap('logo.ico') looks for the icon, it is not there. With the command -i 'C:\temp\Util\logo.ico' you are telling pyinstaller to set that icon file as the windows icon, but this is not the same as including it in the package. To do that try including this in your pyinstaller command:
--add-data "C:\temp\Util\logo.ico;."
The "." after the semi-colon tells pyinstaller where to put the file. In this case it will put it into the same folder as the .exe file.
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 want to double click on my Python script (which uses a Tkinter GUI) and I just want it to open the Tkinter window, not the console window.
To do this, I changed the extension from .py to .pyw and this seems to work fine on Windows but when I double click my .pyw file on a Linux machine, it doesn't work. It simply froze and I had to restart my system.
Please suggest a platform-independent solution that would help me to run the Python script without opening the terminal/command prompt.
it's been a while since i tried on linux, but i believe it should be fairly simple, firstly you need to put a shebang at the top of the script so your shell knows which executable to use:
#!/usr/bin/python
or if you want a specific version you can expand this to:
#!/usr/bin/python3.2
using whichever version you want (only works for first 2 digits)
then you need to mark it as executable:
chmod 711 myfile.py
for more info on this see this page
then when you double click it, on the rpi (last i used linux) it asks if you want to execute it, or execute it in the terminal.
if you choose to execute it without the terminal, you should only see the tkinter GUI
You can use pyinstaller to create the executables for the different platforms you want.
For example,
The syntax of the pyinstaller command is:
pyinstaller [options] script [script ...] | specfile
In the most simple case, set the current directory to the location of your program myscript.py and execute:
pyinstaller myscript.py
PyInstaller analyzes myscript.py and:
Writes myscript.spec in the same folder as the script.
Creates a folder build in the same folder as the script if it does not exist.
Writes some log files and working files in the build folder.
Creates a folder dist in the same folder as the script if it does not exist.
Writes the myscript executable folder in the dist folder.
In the dist folder you find the bundled app you distribute to your users.
Normally you name one script on the command line. If you name more, all are analyzed and included in the output. However, the first script named supplies the name for the spec file and for the executable folder or file. Its code is the first to execute at run-time.
For certain uses you may edit the contents of myscript.spec (described under Using Spec Files). After you do this, you name the spec file to PyInstaller instead of the script:
pyinstaller myscript.spec
You may give a path to the script or spec file, for example
pyinstaller options... ~/myproject/source/myscript.py
or, on Windows,
pyinstaller "C:\Documents and Settings\project\myscript.spec"
pyinstaller