-
Notifications
You must be signed in to change notification settings - Fork 101
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
Allow statically linked Python like PythonCall? #496
Comments
That would be so awesome! |
Here is cjdoris's response to marius311 on the topic:
Indeed, he's right:
|
@mkitti so PyCall/pyjulia could do that as well? |
I think so. We technically just need the pointer. |
Oh that would be awesome! I guess Packages like PySr (@MilesCranmer), diffeqpy (@ChrisRackauckas) and so on would profit a lot from that as well. |
I would like to review the situation here. Part of the issue is that pyjulia is only half of the equation here. The other half is PyCall.jl. In JuliaPy/PyCall.jl#612, they were trying to load the In the linked comment above, @cjdoris demonstrates that we do not need to load python executable or libpython since we could just reuse How is
|
That's cool! I just took a quick look from JuliaCall and it's true
Plus the behaviour of |
While I agree that The use of |
Actually |
You're right, I concede the point. https://docs.python.org/3/library/ctypes.html#ctypes.PyDLL._handle Also ccall(:jl_load_dynamic_library, Ptr{Cvoid}, (Ptr{Nothing},UInt32,Cint), C_NULL, RTLD_GLOBAL, Cint(1)) That does work. xref: JuliaLang/julia#22318 |
On macOS In [1]: import ctypes
In [2]: ctypes.pythonapi._handle
Out[2]: 18446744073709551614
In [3]: hex(ctypes.pythonapi._handle)
Out[3]: '0xfffffffffffffffe' This is actually the value of
|
🤯 I've never actually tried JuliaCall on Mac. I wonder if it works. I should really set up tests and CI. Edit: It works fine! And indeed the handle is that special value. That very last sentence ("this can be a costly search") may explain why loading in ~100 symbols takes so long in PythonCall (~1sec), one reason why PyCall is much faster to load. |
On macOS, you can just In [1]: from julia.api import LibJulia
In [2]: api = LibJulia.load()
In [3]: api.init_julia()
In [4]: api.jl_eval_string(b"""
...: import REPL;
...: term = REPL.Terminals.TTYTerminal("dumb", stdin, stdout, stderr);
...: repl = REPL.LineEditREPL(term, true);
...: REPL.run_repl(repl);
...: """)
julia> python_path = ccall(:_dyld_get_image_name, Cstring, (UInt32,), 0) |> unsafe_string
"~/miniforge3-x86_64/envs/pyjulia_test_x86_64/bin/python3.11"
julia> python_handle = dlopen(python_path)
Ptr{Nothing} @0x000000021ba297e0
julia> Py_IsInitialized = dlsym(python_handle, :Py_IsInitialized)
Ptr{Nothing} @0x0000000104d78020
julia> ccall(Py_IsInitialized, Cint, ())
1
julia> @btime dlsym(python_handle, :Py_IsInitialized)
253.612 ns (1 allocation: 16 bytes)
Ptr{Nothing} @0x0000000104d78020
julia> RTLD_DEFAULT = Ptr{Nothing}(0xfffffffffffffffe)
Ptr{Nothing} @0xfffffffffffffffe
julia> @btime dlsym(RTLD_DEFAULT, :Py_IsInitialized)
270.565 ns (1 allocation: 16 bytes)
Ptr{Nothing} @0x0000000104d78020 PyCall.jl does a lot of symbol loading during precompilation. That is also going to make it difficult for using this pointer though and is also why it doesn't work with a statically linked python executable unless My thought is that this could benefit from a lazy symbol loading scheme such as the one I put into GR.jl: |
Yeah thanks, I've got something similar in a branch somewhere.... |
I'm not an expert and don't know the internals, but is there a reason PyCall can't do whatever PythonCall / juliacall does that lets the user use any Python executable, including ones with a statically linked libpython? Is there anything preventing what they're doing to be used here? A probably related question posted here: JuliaPy/PyCall.jl#988
The text was updated successfully, but these errors were encountered: