Python LOC, .exe size, and refactoring

Steven D'Aprano steve+comp.lang.python at
Wed Feb 22 06:29:27 CET 2012

On Tue, 21 Feb 2012 19:51:07 -0800, CM wrote:

> I have an application that I was hoping to reduce a bit the size of its
> .exe when "packaged" with py2exe.  I'm removing some Python modules such
> as Tkinter, etc., but now wonder how much I could size I could reduce by
> refactoring--and therefore shortening--my code.

Well that will depend on how much you refactor it, but frankly, unless 
your code is truly awful, this will be a micro-optimization. py2exe 
bundles a Python runtime environment plus your files into a single exe 
file. Typically the runtime environment will be somewhere around 11MB for 
wxPython GUI apps (or 4MB with compression turned on, which will slow 
your application down).

The runtime environment for Oracle's Java environment starts at 7MB and 
is typically 15MB, plus whatever libraries your own code produces. For 
dot-net applications, the framework can be up to 60MB.

While I think 60MB for a basic calculator app is taking the piss, this is 
2011 not 1987 and we don't have to support floppy disks any more. 11MB 
for a GUI app is nothing to be worried about. That takes, what, 3 minutes 
to download even on a 512 kbps link? 

> Is there a rule of thumb that predicts the relationship between the
> number of lines of Python code and the resultant size of the application
> (leaving aside the size of imported modules)?

Yes. To a close approximation, for most applications:

size of bundled application = (
    size of Python runtime environment + size of libraries used

Your code is most likely insignificant compared to the others.

> Or is there a way to
> roughly estimate how much would refactoring the code as much as I
> reasonably can help?  (For example, in some cases there is some cut and
> paste coding...I know, it's bad).

Look at it this way: take the .pyc file from your code. How big is it? 
Say, it's 200K. That's a BIG file -- the decimal module in the standard 
library is only 152K. Suppose you could cut it in half -- you would save 
100K. Even if you could somehow cut it down to 1K, you've saved less than 
200K. Do you care?

Refactoring your code is double-plus good for maintainability. You should 
do it anyway. But don't do it to shrink an 11MB exe down to 10.8MB.


More information about the Python-list mailing list