Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimizing the size of embedded Python interpreter

I spent the last 3 hours trying to find out if it possible to disable or to build Python without the interactive mode or how can I get the size of the python executable smaller for linux.

As you can guess it's for an embedded device and after the cross compilation Python is approximately 1MB big and that is too much for me.

Now the questions:

Are there possibilities to shrink the Python executable? Maybe to disable the interactive mode (starting Python programms on the command line).

I looked for the configure options and tried some of them but it doesn't produce any change for my executable.

I compile it with optimized options from gcc and it's already stripped.

like image 284
Benny Avatar asked Sep 27 '12 23:09

Benny


2 Answers

If you reallly want to shrink python, you can have a look at those project :

http://code.google.com/p/python-on-a-chip/

as said on the site :

Python-on-a-Chip (p14p) is a project to develop a reduced Python virtual machine (codenamed PyMite) that runs a significant subset of the Python language on microcontrollers without an OS. The other parts of p14p are the device drivers, high-level libraries and other tools. Please join the python-on-a-chip google group to discuss this project.

This may be a little lower level than expected, but it really is shrinked :

Features of the PyMite VM:

  • Requires roughly 55 KB program memory
  • Initializes in 4KB RAM; print "hello world" needs 5KB; 8KB is the minimum recommended RAM.
  • Supports integers, floats, tuples, lists, dicts, functions, modules, classes, generators, decorators and closures
  • Supports 25 of 29 keywords and 89 of 112 bytecodes from Python 2.6

You could get interesting ideas from this project.

Beware that you only get the virtual machine, ie. you can run pyc files, not py. (pyc files are cross platform, though. And much smaller than py files)

In fact, if you really target smallest python, you need to disable parts of the library, but maybe also the compiler itself.

This site might have interesting pointers also : http://www.awaretek.com/pymo.html

like image 86
makapuf Avatar answered Oct 21 '22 13:10

makapuf


There may be ways you can cram it down a little more just by configuring, but not much more.

Also, the actual interactive-mode code is pretty trivial, so I doubt you're going to save much there.

I'm sure there are more substantial features you're not using that you could hack out of the interpreter to get the size down. For example, you can probably throw out a big chunk of the parser and compiler and just deal with nothing but bytecode. The problem is that the only way to do that is to hack the interpreter source. (And it's not the most beautiful code in the world, so you're going to have to dedicate a good amount of time to learning your way around.) And you'll have to know what features you can actually hack out.

The only other real alternative would be to write a smaller interpreter for a Python-like language—e.g., by picking up the tinypy project. But from your comments, it doesn't sound as if "Python-like" is sufficient for you unless it's very close.

Well, I suppose there's one more alternative: Hack up a different, nicer Python implementation than CPython. The problem is that Jython and IronPython aren't native code (although maybe you can use a JVM->native compiler, or possibly cram enough of Jython into a J2ME JVM?), and PyPy really isn't ready for prime time on embedded systems. (Can you wait a couple years?) So, you're probably stuck with CPython.

like image 3
abarnert Avatar answered Oct 21 '22 13:10

abarnert