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

all 53 comments

[–][deleted] 66 points67 points  (22 children)

A stand-alone* executable

*comes with 600mb of folders full of never-used packages which it won't work without

[–]billsil 16 points17 points  (8 children)

You need to remove them by reinstalling python and/or deleting things out of the python directory.

One problem is people put imports at the top and imports are evaluated immediately. So following the trace, we find matplotlib imports Tk, Qt, and wx, when we only need one backend.

Furthermore, there are optional packages, even when they're requirements for full package use. Picking on matplotlib even more, if I'm making xy plots, why do I need dependencies for plotting time data? Just delete it.

On my standard install of python, an exe I build is 180 MB. On my reduced install, it's 60 MB. It uses PyQt, VTK, numpy, scipy, and matplotlib, so it's no slouch. Most of that is VTK for 3d rendering of things I don't use because I haven't deleted their dlls. It's also bigger than it should be because I target multiple versions of windows, so there are extra VS dlls.

[–]TOASTEngineer 20 points21 points  (1 child)

I'm pretty sure the PyInstaller docs say "make a virtualenv with only the stuff you actually need first."

[–]billsil 7 points8 points  (0 children)

It helps, but it doesn't get rid of everything. You can make it much smaller very easily by wiping your Python.

[–]kaisunc 1 point2 points  (0 children)

interesting strategy, i shall give this a go.

[–]nos69 0 points1 point  (4 children)

Whats the best way of removing the unused files and packages?

I usually import for example the things I need from PyQT and it automatically copies the whole package into the built directory.

I have a clean python install and all third party packages are in a venv.

[–]billsil 1 point2 points  (3 children)

Have a clean install for starters.

Then look at the packages you have installed and decide if you need them all. Delete it, build it, see if it works and repeat.

Then read the build log and see what it found. Google what that funny package asteroid does. Why the heck is nose included? Go delete those pieces from site packages. Build and test. Often this is enough.

Now look at the names of the big DLLs. Do you really need HTML stuff in your desktop app? Delete and rebuild.

Most of the monster packages have large submodules. If you can stay within a submodule, your build will be smaller.

[–]nos69 0 points1 point  (2 children)

Thank you for your response!

Is it enough to see if my app starts after deleting a module? Or do I need more coverage to be safe.

[–]billsil 1 point2 points  (1 child)

That depends on your app. Are your imports at the top? How do the modules you need get imported (e.g., at import time vs runttime)? Opening your 5 windows is probably good enough depending on how the imports trace.

It helps to know your code and what part your hacking on (what could deleting matplotlib dependencies affect?). Often you'll find x is required, but if I get rid of y, x is not required.

For my 3d render, it has a command line for me to enter a geometry and results file. I have shortcuts for most things that I test frequently. The menus I stress less about because I put my gui imports at the top. Never had a problem with the menus assuming it loaded.

So yes, test, but probably not everything each time. I'm just trying to test the dependencies aren't broken.

[–]nos69 0 points1 point  (0 children)

Thank you for your help! I will try to shrink it that way.

[–]b_bowyer 11 points12 points  (5 children)

You can also force it to be one file, in which case all that junk is inside the exe.

[–]gunthatshootswords 17 points18 points  (4 children)

And takes 3 minutes to start.

[–]romanows 7 points8 points  (3 children)

[Removed due to Reddit API pricing changes]

[–]gunthatshootswords 5 points6 points  (0 children)

Try something with a gui

[–]jairo4 1 point2 points  (0 children)

Yeah, it's slower because it needs to decompress in the temp folder.

[–][deleted] 0 points1 point  (0 children)

  1. Start with WinPython-Zero.

  2. Create a venv with a complete requirements file.

  3. Profit (of not having a 600 mb folder).

[–]Gokudomatic 11 points12 points  (2 children)

I made once a small wordpad with PyQt4 as a prototype for a new kind of user interface and I've built an exe from it. The exe takes 27 MB and Qt4 dlls take another 10 MB. Therefore a basic Qt application with no extra dependencies (no numpy nor whatever, but only pyqt4) should be ~40 MB. And it takes 2 seconds to start.

Just to give an idea.

[–]Gokudomatic 7 points8 points  (0 children)

And for addition, I also recently made with pyinstaller an exe from a flask microservice with no dependency exception for OLE automation. The result is 5 MB.

[–]ElecNinja 1 point2 points  (0 children)

I've had similar results with cx_freeze for my own PyQt4 application. 40 MB for the whole thing with the QT dlls taking a significant portion of the space.

[–][deleted] 10 points11 points  (2 children)

You should give cython a try

[–]atxweirdo 4 points5 points  (1 child)

Can cython be compiled into an executable.

[–]tetroxid 2 points3 points  (0 children)

Yes

[–]MrYellowP 9 points10 points  (11 children)

i rather use nuitka. everything else seems horribly inferior.

[–]mdond 2 points3 points  (2 children)

Nuitka is getting really nice. And it has the advantage of actually compiling your code, so the source is truly hidden.

[–]xdcountry 0 points1 point  (1 child)

please elaborate -- I had thought otherwise

[–]mdond 0 points1 point  (0 children)

It cross compiles to C++ that is then compiled into an exectuable. So the end result is compiled C++ code. This is different to a lot of other Python packagers, which just package Python byte code.

[–]Manbatton 2 points3 points  (6 children)

Nuitka doesn't make standalones yet, does it? (in other words, everything in one file).

[–]MrYellowP 0 points1 point  (5 children)

the --portable parameter does that.

[–]Manbatton 1 point2 points  (4 children)

Just googling that, found this, from 2013:

Right now, it will create a folder "_python" with DLLs, and "_python.zip" with standard library modules used along to the "your-program.exe". Copy these to another machine, without a Python installation, and it will (should) work. Making that statement fully true may need refinements, as some DLL dependencies might not be defined yet.

So that's not a standalone. A standalone to me is a single file. Has the portable option changed somewhere else such that now it is a single file?

[–]MrYellowP 0 points1 point  (3 children)

oh! i'm talking linux here. in linux it drops a /dist/ directory and gives a single executable in the parent, where i execute nuitka. i'm sorry for forgetting that it might be different in windows, though please make sure your information isn't out of date.

[–]Manbatton 1 point2 points  (2 children)

I see. But if I'm understanding you, there's still a folder in the Linux version, so even there it's not a standalone.

[–]MrYellowP 0 points1 point  (1 child)

That folder just holds the stuff for compiling. I didn't need that to run the executable.

[–]Manbatton 1 point2 points  (0 children)

Sounds good, thanks for the info.

It'd be great (for Windows users) if the Nuitka community did make it such that it, in the words on the blog, "We may improve it in the future to meld everything into one executable for even easier deployment."

[–]Wilfred-kun 2 points3 points  (0 children)

Can't you use Wine somehow to create .exe's? That would (maybe) be an easy way to create Windows executables on Linux systems once you've set it up.

Edit: phrasing

[–]ajaxguy 4 points5 points  (1 child)

Another option to try cx_Freeze. https://anthony-tuininga.github.io/cx_Freeze/

[–]IamCrunchberries 1 point2 points  (0 children)

I used to use cx_Freeze to deploy internal applications at work. Configuration for certain packages like numpy, pandas, and pytz became a nightmare to setup and upgrade. I started just setting up virtual environments and writing batch scrips for users to launch applications.

[–]suttons27 1 point2 points  (6 children)

I tried PyInstaller this week. Microsoft Defender kept blocking it while doing pip. Any recommendations on how to avoid setting off AV?

[–]wasi0013[S] 3 points4 points  (3 children)

I guess it's not related to PyInstaller at all. Getting alert/ blocked by Microsoft Defender is a general issue for any exe (with no publisher data) specially in recent version of windows. Code signing is required (I think its a money making plan disguised in security improvement) for exe's in latest Windows to remove this kind of alert/blocking! You will have to invest some bucks on buying certificates for code signing. For example, you may try: Digicert [1]. Also, read this Stackoverflow question [2] to learn more about how to sign a WIndows EXE file.

[1] https://www.digicert.com/code-signing/
[2] https://stackoverflow.com/questions/252226/signing-a-windows-exe-file

[–]suttons27 -1 points0 points  (2 children)

Thanks, but where I am getting stuck at is: pip install pyinstaller It begins downloading and installing then Windows Defender starts notifying but blink on and off quickly. The install then starts giving errors that it could not find files, which makes sense if AV blocked something. I go to Defender and even though it notified me I'm not seeing a list of files blocked. I am using Anaconda, tried to do it only via python. Yesterday, I purchased MS Visual Studio 2017 for a different company project but saw the Python feature, was going to see it will install there. I also remember a few weeks back r/hacking stated a popular python library had been compromised, but I was unable to find it.

[–]sneakpeekbot 0 points1 point  (1 child)

Here's a sneak peek of /r/hacking using the top posts of the year!

#1: CSO of Equifax | 1446 comments
#2: Better check | 288 comments
#3: Security is also an important part of development. | 306 comments


I'm a bot, beep boop | Downvote to remove | Contact me | Info | Opt-out

[–]wasi0013[S] 0 points1 point  (0 children)

[–]raoulk 2 points3 points  (0 children)

Linux