[Python-ideas] Calling python from C completely statically

Alberto Garcia agarciaillera at gmail.com
Sun Jul 8 04:01:32 EDT 2018


I've been working for a while on having the entire python interpreter with
all his modules statically linked in a binary that can be called with
arbitrary argument which will be passed to the python interpreter.

I've been able to statically compile Python in a single binary with no
dependencies for Python2.7 and using using this code to call the
interpreter with arbitrary code:

#include <Python.h>

int main(int argc, char** argv)
Py_NoSiteFlag = 1;

This program compiles and does not rely in any dependency but the "non
frozen" (.py) modules are not loaded so when I do a simple `import random`
I get:
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named random

I've created a bug issue (https://bugs.python.org/issue34057) speaking
about it and Nick Colghlan pointed:

" cx_freeze is an illustrative example to look at in that regard, as it
preconfigures the interpreter to be able to find the cx_freeze generated
zip archive that has the program's Python modules in it:

The technique that cx_freeze doesn't use yet is to combine the statically
linked Python binary and the generated zip archive into a single file
(similar to what zipapp does), and adjust the sys.path definition inside
the binary to refer back to the executable itself (since executable files
can have arbitrary content appended, while zip files can have arbitrary
content *pre*pended). "

As I understand, cx_freeze creates a zip file with the dependencies for a
specific python code and creates a zip with them. My question is, could I
create a zip file with every standard module (plus some extra that I may
install with pip) and using that zip file from C? If so, how can I do that?

I'm happy to work on this if I get indications.

BTW: I do not want to convert a python code snippet to exe or anything like
that but calling python from C in an absolutely standalone version.

Thank you

Alberto García Illera

GPG Public Key: https://goo.gl/twKUUv
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180708/9ce4d5cf/attachment.html>

More information about the Python-ideas mailing list