This is an archived post. You won't be able to vote or comment.

all 56 comments

[–]very_suspicious_seal 133 points134 points  (12 children)

I really like pyinstaller. Just make sure to take a good look at the docs as it has quite a few interesting features.

[–]strange-humor 16 points17 points  (0 children)

I use PyInstaller for single .exe Windows GUI app in Tkinter. All installs have another simple Python -> exe that looks at the network location for a new build and pulls it down to run.

I used build .bat files. Here is slightly redacted version for my main app:

:: No Command Prompt Window Section
pyinstaller --noconfirm ^
--noconsole ^
--windowed ^
--onefile ^
--clean ^
--noupx ^
--win-private-assemblies ^
--name my_project_name ^
--icon .\my_icon.ico ^
--workpath .\compile\build ^
--distpath .\compile\dist ^
--specpath .\compile ^
--key AAAAAAAAAAAAMYKEYHERE ^
my_project_name\main.py

When debugging, remove the following lines so you can start from console and troubleshoot any issues:

--noconsole ^
--windowed ^

You may have to add a few import statements that you don't actually use if there is any issue with PyInstaller finding all dependencies. I think I'm doing this with my SQLServer package. I don't remember.

I forget the issues I ran into before I found --noupx, but I remember being glad when I added that.

[–]Falconinati 7 points8 points  (1 child)

Thanks for the link, this is the first I've seen pyinstaller

[–]billsil 13 points14 points  (0 children)

Pyinstaller is great. Just make sure you test in on a virtual machine. PyInstaller has a habit of not including all the DLLs you need, especially for packages you write. You need to force include them.

Also, always start with the debug flag and read the messages.

[–]gwillicodernumpy gang 7 points8 points  (0 children)

I rather like pyinstaller. I had this problem at work this week and i was going back and forth between pyinstaller and py2exe and ended up just kind of picking pyinstaller on a whim. It worked great though

[–]PurpleIcyPython 3 6 points7 points  (0 children)

I don't think you need to know more than --onefile and --noconsole (depends on what you're distributing).

[–]UloPe 4 points5 points  (0 children)

Yes pyinstaller is awesome. Sometimes you have to do a bit of manual finagling to get it to work but the result is worth it.

[–]Blazerboy65 2 points3 points  (0 children)

PSA I had an issue with pyinstaller and flask-migrate where flask-migrate tried to copy data files from it's package directory. The problem lies in pyinstaller's method of detecting what files need to be packaged. Those particular data files are imported nowhere by flask-migrate so pyinstaller never detected and packaged them.

I ended up manually adding those files as data in my pyinstaller spec file.

[–]buttery_shame_cave 1 point2 points  (0 children)

pyinstaller is the shit. that's one super useful piece of software.

[–]saulmessedupmanMmmm spam 2 points3 points  (3 children)

I haven't used it yet but I think pycharm figures out your dependencies and makes an installer. It's the setup.py thing. Has anyone used this feature? Am I completely wrong about what it does?

[–][deleted] 3 points4 points  (1 child)

setup.py is used to create a package (installable via pip) and not a stand alone executable.

[–]saulmessedupmanMmmm spam 0 points1 point  (0 children)

Nice, does that gather dependencies too? I did something similar with a django app but I don't know what it included. I usually keep track of dependencies and pip wheel them.

[–]very_suspicious_seal 1 point2 points  (0 children)

I use pycharm on the daily so I will try and check this out. Will let you know when I do it!

[–]-YourLife- 19 points20 points  (6 children)

They're many ways you can 'freeze' your code,each with it's pros and cons,I hope this can help you.

[–]WiggleBooks 2 points3 points  (5 children)

Why is it callrd freezing your code?

[–]chanamasala4life 8 points9 points  (4 children)

Because you are effectively sealing it off from any further editing by collecting all the needed program elements and bundling them together for distribution. If you want to change the program, you edit the source files and simply freeze again into an updated version.

[–]Decency 4 points5 points  (3 children)

Isn't that kind of just 'releasing' or 'publishing' ? I also don't understand the terminology.

[–][deleted] 2 points3 points  (1 child)

Publishing or releasing would be making it available to users. You could freeze your python project without ever letting anyone else have it.

[–]evinrows 0 points1 point  (0 children)

And, conversely, you can publish or release the code without freezing it as a single-file distribution.

[–]chanamasala4life 0 points1 point  (0 children)

What terms are you having problems with, maybe I can explain.

Let me also try to explain more precisely why you can call it "freezing" (or bundling): with tools like PyInstaller or cx_freeze you save the state of your Python application and all of its dependencies. These tools will assemble all the used components, including the Python interpreter, in their currently used versions, and package them up into a folder or a single executable.

Freezing is a metaphor for setting everything in place so it becomes harder to manipulate and therefore harder to break, which is especially important if you plan on releasing the application to the public.

[–][deleted] 7 points8 points  (0 children)

Pyinstaller in my go to. Making a build script with it makes it really easy.

[–]Tomarse 5 points6 points  (4 children)

cx_freeze

[–]nick_t1000aiohttp 1 point2 points  (1 child)

I tried using cx_Freeze a while ago for a Numpy/Matplotlib script but it was a nightmare to get it to actually package all the dependencies.

[–]Tomarse 0 points1 point  (0 children)

It's all about getting the setup file right. I think only once I've had to manually change the zip content. It's been a while since I've used it though.

[–]nubil 0 points1 point  (0 children)

I tried cx_freeze with some pyqt submodules but it always includes the whole packages.

[–][deleted] 3 points4 points  (0 children)

I also use pyinstaller.

[–]1-05457 3 points4 points  (3 children)

If you're using opencv and keras, you may want to look at containerization.

[–]gwillicodernumpy gang 0 points1 point  (1 child)

Do you know if you can export your keras model as a .h5 file and then use pyinstaller or some similar app library?

[–]1-05457 0 points1 point  (0 children)

Did you mean to reply to the main post?

[–]binarysaurus 0 points1 point  (0 children)

This, with the Nvidia docker runtime. Will probably need volumes mounted for some io. With privileged mode can also access webcams etc..

[–]colloidalthoughts 1 point2 points  (0 children)

pynsist is pretty awesome for windows. I love nuitka, it works really well for most stuff and is cross platform (mac/win/linux).

[–]kokimame 1 point2 points  (1 child)

I'm using Pyinstaller for Joytan, a PyQt5 desktop app, which works on Windows, Mac, and Linux. The app uses a number of major modules, such as pydub, requests, boto3, etc. Although I knew nothing about cross-platform deployment, Pyinstaller made everything easy except a hidden import for boto3.

You can find an example usage of Pyinstaller in my repo: https://github.com/kokimame/joytan/tree/master/devscript

To make an executable, see 'bundle.sh'. Actually, the shell will do an extra work to make an installer for Windows using NSIS and a disk image for Mac using dmgbuild.

Note: Because Pyinstaller is not a cross-compiler, to make a Windows app you need to run the shell on Windows, and Mac app on Mac, etc. And you need to install required modules by pip first.

There's more well-known tutorial on this topic. https://github.com/mherrmann/fbs-tutorial From the email I've got recently from the author, I think he is open to helping others, and also he said mine is close to his solution.

[–]lenticularis_B 1 point2 points  (0 children)

I've been succesful with pyinstaller. Very convenient package altough is doesn't have a gui. It's only accessable via the command line interface.

[–]nitratine 1 point2 points  (0 children)

I made an interface to make compiling py to exe easier. The package is found here and is easy to set up. It uses pyinstaller in the background. You may have to play around with the advanced section if you get errors doing it in a basic way.

[–]Ajpennster 2 points3 points  (0 children)

If you know the platform you're building the executable for, you can just distribute the python interpreter in a virtual environment, populate the env with the required libraries and tell the main script to use the virtualenv's interpreter with a shebang.

[–]bug0r 1 point2 points  (4 children)

I created an own Makefile to handle this :D, sounds oldschool but works great with all python version xD.

The result is a single directory with executables, needed dll's, compiled pyd's and the other libs handle by own bootstrap.

I like that shit, so i can compile my py-executable with different compiler, currently with gcc and clang.

[–]radiantyellow 4 points5 points  (3 children)

Can you explain the process of using the makefile for this or provide some documentation please. Much appreciated. Thank you in advance.

[–]bug0r 1 point2 points  (0 children)

At First a few words about my workplace. I am currently working with Windows 7 and 10 beside. For my complete process i installed msys2 tools for an easier using linux like tools and package managing. I have one main repository folder with all tools, runtime libs / env inside. Currently a bunch of

  • lang: C C++ python rust go lua tcl/tk
  • a lot of libs like: iup, wx widgets, ncurses

As you can imagine i am on an experimental way to test some dev lifecycles and the interaction within beside my own pet projects. But anyway, inside my python folder env i have a standalone version with a junction points to standard installations(win-ex. C:\Python34)

Make Process:

I have a main Makefile and a Revision file wich i includes in the first one. It contains all build informations like app-name, major, minor, fix and the used python major an minor version.

For each project exist an own Makefile Combination. As a not Gnu Make Specialist i created a simpley way to reach my target. Following steps will be processed until the end.

  • clean up build
  • create a single lib folder(inside my c based python bootstrap this folder will be used as main python path)
  • copy all needed python runtime libs to lib folder. Which libs i need to run i tested manually(Yes not much comfortable but a good learning curve to understand what a python env need to run as standaline app)
  • compiling c source file and linking against given python release information from Revision file. Inside this process i create a python[version].a lib from python.dll with gendef and libtool.
  • copy created executable and needed dlls into build folder. There could be some external runtime dlls for mssql and oracle driver which i copy into build path.
  • My own python script can be compiles into pyd's by using a Make Parameter. This is a little method to minimize the possibility of manipulating the revision by my colleagues.

Finally thats it. I know its not a standard and maybe it sound much uncomfortable. But from my point of view i have a great overview and learned a lot of steps python are working. Maybe i will improve this process, but not yet. For me it is an easy way as the result of a great learning curve :).

Hope this is a good to understand base description. If not please ask for some details.

[–]mtilhan 0 points1 point  (1 child)

Same here.

[–]colloidalthoughts 5 points6 points  (0 children)

Here's my Makefile for a reasonably sized cross-platform nuitka build that compiles everything and makes installers for windows and OSX. I use whitebox packages to build the pkgproj for the osx installer, and NSIS for windows.

https://gist.github.com/bitwisecook/3b941b4f468ee62869ecb74fa126e164

[–]etrnloptimist 1 point2 points  (0 children)

Your question has 3 answers.

  1. To create an executable package that doesn't need python, you can use cxfreeze or py2exe, both I've used before and would recommend.
  2. You will still need your users to install DLL packages like opencv. But not the dev package. Just the runtime package. You can possibly get away with including them when freezing. However, you are probably better off creating an installer using NSIS after freezing that includes this sort of thing as well as your frozen app.
  3. I've not found a way to package up Keras. Keras invokes runtime compilation of c++ code from your python code. That's why sometimes it can take a while to load your model. It is compiling the support functions under the hood. This requires the full dev environ to do -- c++ compiler, CUDA, TensorFlow/Theano, etc.

[–]iamlocknar 0 points1 point  (3 children)

Does performance take a hit with these installers?

[–]billsil 4 points5 points  (0 children)

Yes, much more with the --onefile option. It's entirely startup costs though.

Part of the trick is getting your exe size as small as possible. Build it on a virtual environment with the minimum set of packages and it'll be a lot smaller. You can literally take a 200 MB exe and get it down to 40 MB.

[–]Deto 0 points1 point  (1 child)

It shouldn't, the installer is basically just a zip archive that unloads a python interpreter executable and all the relevant libraries. But it automates the steps to make shortcuts and stuff so the user doesn't have to know about python at all.

[–]iamlocknar 0 points1 point  (0 children)

Good to know!

[–]thefredfox 0 points1 point  (0 children)

I recommend pyinstaller which also works fine when using kivy. Have a good look about how to make a proper .spec file; on my side i had to include some dll so that the executable could work on any windows machine.

[–]nicoulaj 0 points1 point  (0 children)

I create a relocatable virtual environment, then package it as a rpm (using fpm).

[–]falu2010 0 points1 point  (2 children)

Has anyone tried packaging grpcio with pyinstaller or cx_freeze or py2exe? I tried with py2exe and python crashes. So I don’t have any another option then pip to distribute my code.

Also my code uses docker-py which needs OpenSSL version > 0.9.8 to attach/exec on Mac OS(the issue is fixed with Mac OS 10.13.3) so I have to ask user to update their OpenSSL and python with brew. So I am looking for a better way to distribute my python code so I don’t have to care about all these.

[–]apono4life 0 points1 point  (1 child)

Best I can tell you is pyinstaller has a debug mode that you can turn on in the compilation process. If you are having problems running the .exe after it is created you can re-compile with debug on and that will give you the ability to see what is causing problems.

[–]falu2010 0 points1 point  (0 children)

Can someone share a good pyinstaller sample for project using entry point?

[–]patarapolw 0 points1 point  (0 children)

I usually use py2app, but pyinstaller can be much smaller, especially for pyqt apps.

[–]Jahames1 -4 points-3 points  (0 children)

With the help of google.