[ACCEPTED]-Compile the Python interpreter statically?-compilation

Accepted answer
Score: 36

I found this (mainly concerning static compilation 28 of Python modules):

Which describes a file 27 used for configuration located here:

<Python_Source>/Modules/Setup

If 26 this file isn't present, it can be created 25 by copying:

<Python_Source>/Modules/Setup.dist

The Setup file has tons of documentation 24 in it and the README included with the source 23 offers lots of good compilation information 22 as well.

I haven't tried compiling yet, but 21 I think with these resources, I should be 20 successful when I try. I will post my results 19 as a comment here.

Update

To get a pure-static python 18 executable, you must also configure as follows:

./configure LDFLAGS="-static -static-libgcc" CPPFLAGS="-static"

Once 17 you build with these flags enabled, you 16 will likely get lots of warnings about "renaming 15 because library isn't present". This 14 means that you have not configured Modules/Setup correctly 13 and need to:

a) add a single line (near the 12 top) like this:

*static*

(that's asterisk/star the 11 word "static" and asterisk with 10 no spaces)

b) uncomment all modules that 9 you want to be available statically (such 8 as math, array, etc...)

You may also need 7 to add specific linker flags (as mentioned 6 in the link I posted above). My experience 5 so far has been that the libraries are working 4 without modification.

It may also be helpful 3 to run make with as follows:

make 2>&1 | grep 'renaming'

This will show 2 all modules that are failing to compile 1 due to being statically linked.

Score: 10

CPython CMake Buildsystem offers an alternative way to build Python, using 4 CMake.

It can build python lib statically, and 3 include in that lib all the modules you 2 want. Just set CMake's options

BUILD_SHARED                     OFF
BUILD_STATIC                     ON

and set the 1 BUILTIN_<extension> you want to ON.

Score: 6

Using freeze doesn't prevent doing it all 14 in one run (no matter what approach you 13 use, you will need multiple build steps 12 - e.g. many compiler invocations). First, you 11 edit Modules/Setup to include all extension modules that 10 you want. Next, you build Python, getting 9 libpythonxy.a. Then, you run freeze, getting 8 a number of C files and a config.c. You 7 compile these as well, and integrate them 6 into libpythonxy.a (or create a separate 5 library).

You do all this once, for each 4 architecture and Python version you want 3 to integrate. When building your application, you 2 only link with libpythonxy.a, and the library 1 that freeze has produced.

Score: 3

You can try with ELF STATIFIER. I've been used it before 5 and it works fairly well. I just had problems 4 with it in a couple of cases and then I 3 had to use another similar program called 2 Ermine. Unfortunately this one is a commercial 1 program.

More Related questions