Skip to content Skip to sidebar Skip to footer

Pack Python Library To One File

I want to pack pure python library into one source python script. (not exe, app, etc. I need exactly lib.py file) Is it possible? Which tools are exist for this?

Solution 1:

Best is to pack all your python files into a zip: https://blogs.gnome.org/jamesh/2012/05/21/python-zip-files/

If you really want to, sure, it is possible to pack everything (including a zip I think) to a .py file, just write your custom importhook, store base64-encoded zip archive as a string in your since python source, a bit of hardcore magic here and there and voila, single python program,

Alternatively you could refactor all your dependencies and include these as classes for example in your source, but why bother?

Finally, there's no simple way to include compiled dependencies, i.e. those .so / .dylib / .dll files.

I don't think there is a tool out there that does exactly what you want, but I reckon pyinstaller/py2exe can be used to get pretty close, for example http://www.py2exe.org/old/ look for library.zip.

Post a Comment for "Pack Python Library To One File"