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
Related
I am trying to compile a python file with Nuitka in stead of Pyinstaller. Everything is going great except for the facts that I do not understand how I should add another data file to the python file.
The problem
If I want to add an image to my python file in Pyinstaller (in Windows) I would do:
wine /root/.wine/drive_c/Python27/pyinstaller.exe --add-data "/root/Downloads/car.jpg;." --onefile --noconsole --icon /root/Downloads/icon.ico pythonfile.py
Now if I would open this exe file I would run the python file and open the car.jpg file.
I want to do something similar using Nuitka. When I looked at the documentation of Nuitka I saw that I probably needed to use the --include-data-file=<source>=<target> argument.
I tried this and it gave no errors, but when I open the created exe file, it does not open the given file. All the other arguments worked as I wanted, so only the --include-data-file argument does not give the wanted result
This is the Nuitka command I tried:
.\python.exe -m nuitka --mingw64 .\pythonprogram.py --standalone --onefile --windows-icon-from-ico=pdf.ico --windows-disable-console --include-data-file=C:\Users\User\AppData\Local\Programs\Python\Python39\*.pdf=mypdf.pdf
My question(s):
Am I using the correct argument?
Is this even possible with Nuitka?
How would I fix my problem?
Thanks in advance!
First of all, when encountering unexpected behaviour in Nuitka (in this case, you'd expect a file to open, which it didn't) I highly recommend removing the --windows-disable-console argument and observing the terminal output; without it debugging the compiled program is virtually impossible. You can also get a lot of information by analyzing Nuitka's output during transpiling and compilation (for example, there may appear a warning informing you about including numpy's plugin in order to add support for the numpy module).
Now, moving on to your questions:
Am I using the correct argument?
Yes, argument --include-data-file=<source>=<target> is correct if what you want is to embed one file.
What is however very important when it comes to programs compiled with --onefile argument is how the files are loaded internally, in the .py files.
I feel like the best thing I can do is show an example of my own (I'm using pygame for my project, but this way of loading files should work in any circumstance):
import os
import pygame
def load_file(file_name: str) -> str:
return os.path.join(os.path.dirname(__file__), file_name)
image_1_sprite = pygame.image.load(load_file("textures/image_1.png"))
I would then use Nuitka as such: python -m nuitka --onefile --include-data-dir=../workingdir/textures=textures main.py or, if you want to include just one file, python -m nuitka --onefile --include-data-file=../workingdir/textures/image_1.png=textures/image_1.png main.py, where workingdir is the directory in which main.py resides (I recommend using relative paths to make the repository portable, because if someone were to download your source code and save it, for example, on the D:\ hard drive - the executed command would fail).
I created a data-miner GUI for twitter with kivy and am currently having a lot of trouble turning it into an exe. I tried following this video and import glew and sdl2 into my spec but after doing pyinstaller main.spec, my executable still would not open.
Is it because I have more than one files and folders for my program (here is the link to the github repo for my project), if so, how do you deal with that?
In addition, if I manage to success create a working exe, how do I create an exe installer that other people can use to install the executable?
Making an executable from a complex script like yours may become quite frustrating because of its dependencies. But I'm giving you a brief guide about what you need to follow to achieve your goal.
Create your main.spec file with console-mode enabled to see the exact error message for the app. (make sure to remove --noconsole from PyInstaller command or set console=True in spec file). Also use --no-upx in the build command to remove compression from output file (this helps with omitting some DLLs which may cause issues).
You need to make sure that every external module you used can pack correctly. I don't think you get any problem with either Kivy or Tweepy. But if you get any missing import error, try to check the solution for each one by searching the pattern [module] pyinstaller.
Your app has external resources like images, files, etc., which must be added to the packed executable and load properly. I wrote an answer about this here.
If you want a standalone executable, you need to use -F with PyInstaller command, which is more robust than using an installer to gather files in one directory mode.
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 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.
This question already has answers here:
Create a single executable from a Python project [closed]
(3 answers)
Closed 3 years ago.
I wrote a script that will help a Windows user in her daily life. I want to simply send her the .exe and not ask her to install python, dlls or have to deal with any additional files.
I've read plenty of the stackoverflow entries regarding compiling Python scripts into executable files. I am a bit confused as there are many options but some seem dated (no updates since 2008) and none were simple enough for me not to be asking this right now after a few hours spent on this.
I'm hoping there's a better, up-to-date way to do this.
I looked into:
pylunch
py2exe
cx_Freeze
py2app (only for Mac)
pyinstaller
bbfreeze
but either I couldn't get them to work or couldn't understand how to get the result I need. The closest I got was with py2exe but it still gave me the MSVCR71.dll
I would appreciate a step-by-step answer as I was also unable to follow some of the tweaking answers here that require some prior understanding of how to use py2exe or some of the other tools.
I'm using Python 2.5 as one of the modules is only available for that version.
PyInstaller will create a single-file executable if you use the --onefile option (though what it actually does is extracts then runs itself).
There's a simple PyInstaller tutorial here. If you have any questions about using it, please post them...
Using py2exe, include this in your setup.py:
from distutils.core import setup
import py2exe, sys, os
sys.argv.append('py2exe')
setup(
options = {'py2exe': {'bundle_files': 1}},
windows = [{'script': "YourScript.py"}],
zipfile = None,
)
then you can run it through command prompt / Idle, both works for me. Hope it helps
i would recommend going to http://sourceforge.net/projects/py2exe/files/latest/download?source=files to download py2exe. Then make a python file named setup.py.
Inside it, type
from distutils.core import setup
import py2exe
setup(console=['nameoffile.py'])
Save in your user folder
Also save the file you want converted in that same folder
Run window's command prompt
type in setup.py install py2exe
It should print many lines of code...
Next, open the dist folder.
Run the exe file.
If there are needed files for the program to work, move them to the folder
Copy/Send the dist folder to person.
Optional: Change the name of the dist folder
Hope it works!:)
I would join #Nicholas in recommending PyInstaller (with the --onefile flag), but be warned: do not use the "latest release", PyInstaller 1.3 -- it's years old. Use the "pre-release" 1.4, download it here -- or even better the code from the svn repo -- install SVN and run svn co http://svn.pyinstaller.org/trunk pyinstaller.
As #Nicholas implies, dynamic libraries cannot be run from the same file as the rest of the executable -- but fortunately they can be packed together with all the rest in a "self-unpacking" executable that will unpack itself into some temporary directory as needed; PyInstaller does a good job at this (and at many other things -- py2exe is more popular, but pyinstaller in my opinion is preferable in all other respects).
1) Get py2exe from here, according to your Python version.
2) Make a file called "setup.py" in the same folder as the script you want to convert, having the following code:
from distutils.core import setup
import py2exe
setup(console=['myscript.py']) #change 'myscript' to your script
3) Go to command prompt, navigate to that folder, and type:
python setup.py py2exe
4) It will generate a "dist" folder in the same folder as the script. This folder contains the .exe file.
you may want to see if your app can run under IronPython. If so, you can compile it to an exe
http://www.codeplex.com/IronPython
You can create executable from python script using NSIS (Nullsoft scriptable install system). Follow the below steps to convert your python files to executable.
Download and install NSIS in your system.
Compress the folder in the .zip file that you want to export into the executable.
Start NSIS and select Installer based on ZIP file. Find and provide a path to your compressed file.
Provide your Installer Name and Default Folder path and click on Generate to generate your exe file.
Once its done you can click on Test to test executable or Close to complete the process.
The executable generated can be installed on the system and can be distributed to use this application without even worrying about installing the required python and its packages.
For a video tutorial follow: How to Convert any Python File to .EXE
You could create an installer for you EXE file by:
1. Press WinKey + R
2. Type "iexpress" (without quotes) into the run window
3. Complete the wizard for creating the installation program.
4. Distribute the completed EXE.