-
Notifications
You must be signed in to change notification settings - Fork 763
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Static Build of pyo3 (embedding python interpreter) #416
Comments
That's currently not supported, though it would be possible. I've written the necessary instructions for linux in the fourth of #276 |
To implement this |
Did anyone have any success related to this? I'm about to try this and any information would be helpful. Will report back with results. |
Hello everyone, I was also interested in this feature. I looked into it and I thought of directly integrating the embedded library provided by python rather than compiling it statically. I think it's much easier to deploy since you just have to download the python library in portable format and integrate it into your own project. To do this, I thought of providing another environment variable allowing to indicate the archive containing the portable python executable. This would then be unzipped into the target/{...}/out folder and automatically linked to the project. Or much more simply leaving the possibility to overload the location of the python library. I think the latter solution would not require much change. Tell me what you think about this? |
Has there been any progress on this? I see https://github.com/PyO3/pyo3/blob/master/build.rs#L333 |
I investigated this briefly, but I think even with the static linking "supported" we have plenty of missing symbol errors which are seen in other tickets #763 #742 . I believe that the problem is that unused symbols get removed at link-time, which is problematic when loading other Python extensions from a statically embedded Python interpreter. I asked about this on URLO, but got no response: https://users.rust-lang.org/t/embed-whole-python-interpreter-using-static-linking/42509 Not sure if to get this working properly we need to add changes to cargo / rustc. |
@davidhewitt Thanks for the update. |
FTR: https://pyoxidizer.readthedocs.io/en/stable/rust.html seems to work well for embedding, although there are some limitations around which native python extension modules can be embedded along with the interpreter. |
IMHO, I think if someone want to distribue application with embedding python interpreter, he/she doesn't need to static link python interpreter with rust-base application. |
If you really do want to embed statically, I've managed to get a very basic POC to function on unix. It might have issues I haven't run across yet; it does successfully build (using nightly) and can import numpy. I had to emit this slew of cargo options in pyo3's
With those in place, I could compile this code:
using this command line:
The resulting program worked like a normal python interpreter:
So what I think about this: while we're still a long way from supporting static linking properly, this at least proves that it is possible. What would need to be improved to make this something that I'd consider ready to ship in PyO3:
Hopefully the notes here serve as a useful reference for the future. |
@davidhewitt Chances are you could get (at least some of) those flags from
I think Rust has official bindings... |
The above is a pretty minimal set of libraries needed by the Py runtime itself. |
Thanks, interesting. You might want to see https://pyo3.rs/v0.15.1/building_and_distribution.html#advanced-config-files |
So, I had a look at how PyOxidizer do what they do. I think this is the best explanation & demo: https://pyoxidizer.readthedocs.io/en/latest/pyoxidizer_rust_generic_embedding.html#embed-python-with-pyembed In summary:
The pyo3 config file generated on my system:
The lib search path contains the native dependencies, statically built:
It's impressive, but at the same time pretty involved and kind of opaque (for example, why use a custom archive format as opposed to eg. tar or such)... |
@vojtechkral, How do you install python external dependencies with pyo3? |
I was reading Statically embedding the Python interpreter and there I found an interesting line:
I just checked
AFAIK, |
They might also just be import libraries: https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-creation#creating-an-import-library |
I ran the following commands C:\Python311\libs>dumpbin /ARCHIVEMEMBERS python3.lib > python3.txt
C:\Python311\libs>dumpbin /ARCHIVEMEMBERS python311.lib > python311.txt and this is the result: It looks like these are indeed import libraries for python3.dll and python311.dll, respectively. |
I have a suggestion for how to handle static linking on Windows. We can package the python.dll file inside the executable, and intercept the loading of pyd files. We can load the Python modules from a resource embedded in the executable, or from a separate zip file. This way, we can achieve a single-file executable. If anyone is interested, I can provide some examples to implement this solution. |
@deadash Did you have a look at PyOxidizer mentioned upstream? |
I would like to run python from rust, but in my situation, end users who will run the program, haven't preinstalled python and it's not possible to install (their system is extra restricted).
Is it possible build a rust program that uses pyo3 will embed python interpreter? (So that users don't need to install a python interpreter separately)?
Is it possible please give me some hit, In documentation there wasn't any point to this.
The text was updated successfully, but these errors were encountered: