making executables smaller

Steven D'Aprano steve at pearwood.info
Tue Jul 26 22:00:41 EDT 2016


On Wed, 27 Jul 2016 03:22 am, Carter Temm wrote:

> Hi,
> I’m writing a couple different projects at the moment, and when I compile
> it into a single executable using pyinstaller, it becomes extremely large.

What do you consider "extremely large"? Ten gigabytes? 500 kilobytes? Give
us a clue.


> I’m guessing this is because of the modules used.

Maybe yes, maybe no.

On my system, Python 3.3 is a little bit less than 6 MB, and the std lib
under 130MB:


[steve at ando ~]$ du -hs /usr/local/bin/python3.3
5.7M    /usr/local/bin/python3.3
[steve at ando ~]$ du -hs /usr/local/lib/python3.3/
129M    /usr/local/lib/python3.3/

But nearly 50MB of that is the test suite:

[steve at ando test]$ du -hs /usr/local/lib/python3.3/test/
48M     /usr/local/lib/python3.3/test/

I expect that on Windows or Mac OS X the sizes will be roughly the same.

I'm not an expert on the pyinstaller internals, but I would expect that it
would be able to drop the test suite, but will need to include the rest of
the std lib as well as the interpreter, plus whatever files you have
written. So I expect that a frozen executable will be of the order of 80MB,
give or take. There's probably a bit of overhead needed to hold it all
together, so let's say 100MB in round figures.

If you're getting less than 100MB, that doesn't sound too bad to me, not for
an interpreted language like Python. Anything less than that sounds really
good to me. If you are getting under 20MB, that's *fantastic*. What's the
problem with that? You can fit close to forty of these on a 4GB DVD-RW or
USB stick. You're not hoping to fit your application on a floppy disk are
you? Its 2016, not 1980.

If you're working with really constrained environments, like an embedded
device, then check out µPy or PyMite:

https://micropython.org/

http://deanandara.com/PyMite/

although I fear PyMite may not be under active development.



> Because I’m not that 
> skilled at python, I put stuff like for example, import sys. I imagine the
> final project could be made smaller by specifying from something import
> something_else. 

No, it doesn't work like that. For starters, sys is built into the
interpreter, so it's unlikely you'll be able to remove it. But more
generally, if you use anything from a module, the entire module must be
included.

Even if *you* don't use a module, perhaps one of the modules you do use will
in turn use that module. 


Of course, you can avoid all this overhead completely by *not* freezing the
executable down to a single file. Just distribute your Python code as a .py
file, or a .pyc file. There are other options as well, such as distributing
it bundled into a zip file. What benefit do you get from using PyInstaller?


> but the thing is, I don’t know what smaller I could import 
> with these set of modules. Is there a program that could tell me this.
> Sorry if this question is really basic, but it’d be helpful.



-- 
Steven
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list