indygreg / python-build-standalone

Produce redistributable builds of Python
BSD 3-Clause "New" or "Revised" License
1.71k stars 107 forks source link

Help embedding a static Python build in a C++ application #163

Closed John-Dean closed 1 year ago

John-Dean commented 1 year ago

Hello,

I've been trying to embed Python in a C++ application I'm writing. I have got it working with a regular Python install, but this requires that the user have it installed on their system - which is something I can't rely on.

Long story short, I went on a bit of a hunt to see if it was possible to completely embed Python within the application, such that the end user would not require it to be installed. After some searching[1], I found that it's possible if Python is compiled statically.

I then got sent down a rabbit hole of compiling Python statically, and ended up here with your static builds.


In the most simple terms this is my issue:

I have a test application mirroring the structure described [here], namely a C++ console application with a single file.

If I add the includes/libraries to point to my normal Python install this works and I can validate it working.

If I now point them to a static build[2] the program first spits out an error about missing the lib file (as it's named python.lib and not python310.lib). If I rename this the error goes away and the program compiles.

However, when I run the program I am met with an error: image

I am at a bit of a loss as to what I am doing wrong (if anything), so if anyone would be able to help with this that would be fantastic.

I have tested building with Visual Studio's compiler and CMake, both output the same error.

Many thanks, John

touilleMan commented 1 year ago

Hi @John-Dean,

I use python-build-standalone to embed python in a shared library, so my usecase is pretty similar to yours ^^ So you can have a look a my project to help you debugging your issue:

John-Dean commented 1 year ago

Thanks @touilleMan,

I've taken a look, so as far as I can tell

https://github.com/touilleMan/godot-python/blob/master/platforms/windows-64/SConscript downloads, extracts and removes pycache and /tests/, then zips the standard library.

and the second file https://github.com/touilleMan/godot-python/blob/master/pythonscript/SConscript handles the compiling.

I noticed your final install (i.e. https://github.com/touilleMan/godot-python/releases/download/v0.50.0/godot-python-0.50.0-windows-32.zip) has /addons/pythonscript/windows-32 and then has the python.dlls, /Lib and /libs and .exe - my understanding is if you statically embed you didn't need all of this.

Perhaps this is just a misunderstanding from me, but I was of the understanding I could embed python without having to bundle all of this.

John

touilleMan commented 1 year ago

Perhaps this is just a misunderstanding from me, but I was of the understanding I could embed python without having to bundle all of this.

This is not possible with python-build-standalone alone (i.e. I doesn't come out of the box) pyoxidizer does some black magic to be able to store all the Python assets in it own binary, so you should have a look there if that's what you want to do.