build a static executable program with python

Adal Chiriliuc nomail at example.com
Thu Dec 30 15:05:05 CET 2004


My app uses Python 2.4 and the Pythonwin GUI (Python MFC wrapper).
I build it as a single exe file with everything linked in it. It's
a lot smaller than all the DLL's put together.

The Python modules are also put into the file as a Win32 resource
which contains all the modules bz2 compressed. I gather the required
modules the same way as py2exe does (using modulefinder), then I
compile them to .pyc files, bundle them all together in one file with
some information (like size and name) and than bzip compress the whole
file. When the app starts it uncompresses them and init's the
PyImport_FrozenModules variable (just like in freeze).

So I only have one exe file to ship, which contains everything needed.
Right now it's size is 2 MB, but bear in mind that it contains STLport
and Crypto++ which are big libraries. It compresses down to under 1 MB.

It's not very difficult to do if you have some experience. But you do
have to tweak some Python header files (to disable for example the
normal link against python24.lib).

You are right about the DLL modules. You just compile tham as a static
lib and manually call their's their's init function, AND ALSO THE
DllMain ONE. The exe file will still export the DLL functions, but you
can disable this with some tweaks.

Hope that helps. I can guide you through the process if you wish.

On Thursday, December 30, 2004 Torsten Mohr wrote:
> Hi,

> i'd like to build an executable file that is linked with
> a python library and executes a script via PyRun_SimpleString
> or similar functions.

> Is there a static library of python available, so the users
> don't need to install python?

> What about DLL modules, do i just need to compile them
> as a static lib, link them together with my program and
> call their init function?


> What about python modules, can i just use a tool like
> "freeze" or "py2exe" to break up the import hierarchy
> and call them before my script?


> Is there some more information about building a static
> executable available?


> Thanks for any hints,
> Torsten.






More information about the Python-list mailing list