making executables smaller

Thomas 'PointedEars' Lahn PointedEars at web.de
Wed Jul 27 00:52:58 EDT 2016


Carter Temm wrote:

> 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.
> I’m guessing this is because of the modules used. 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. 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.

I recommend to comment out all “import” statements (for later reference) and 
then use a Python editor like PyDev to generate step by step “from … import 
…” statements for all used symbols that are not yet defined.

If it is a rather simple program where you can easily make no-ops out of 
modifying statements by defining mockups, you can just run it before you 
compile it to see where you get errors with commented-out “import” 
statements.

In theory, you could look for code of the form “foo.bar” using the regular 
expression "(?:"")?(?:[^"\\]|\\"(?:"")?)+"(?:"")?|'(?:'')?(?:[^'\\]|
\\'(?:'')?)+'(?:'')?)|((?:\w+\.)+)(\w+)¹.  If $1 (e.g., “foo”) is not 
defined in the file, then it is likely that $2 (e.g., “bar”) is a symbol 
from module $1 and you could write

#-----------------
from $1 import $2
…
$2
#-----------------

instead of

#-----------------
import $1
…
$1.$2
#-----------------

But there can be false positives and duplicate symbols this way.

_________
¹  the first two alternatives exclude Python string literals
-- 
PointedEars

Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.



More information about the Python-list mailing list