2

I'm trying to compile a usable .dll file from Julia to be used in Python as I've already written a large GUI in Python and need some fast optimization work done. Normally I would just call PyJulia or some "live" call, however this program needs to be compiled to distribute within my research team, so whatever solution I end up with needs to be able to run on its own (without Julia or Python actually installed).

Right now I'm able to create .dll files via PackageCompiler.jl, something I learned from previous posts on StackOverflow, however when trying to run these files in Python via the following code

Julia mock package


module JuliaFunctions
    # Pkg.add("BlackBoxOptim")
    Base.@ccallable function my_main_function(x::Cfloat,y::Cfloat)::Cfloat
        z = 0
        for i in 1:x
            z += i ^ y
        end
        return z
    end
    # function julia_main()
    #     print("Hello from a compiled executable!")
    # end
    export my_main_function

end  # module

Julia script to use PackageCompiler

# using PackageCompiler
using Pkg
# Pkg.develop(path="JuliaFunctions")  # This is how you add a local package
# include("JuliaFunctions/src/JuliaFunctions.jl")  # this is how you add a local module
using PackageCompiler

# Pkg.add(path="JuliaFunctions")
@time create_sysimage(:JuliaFunctions, sysimage_path="JuliaFunctions.dll")

Trying to use the resulting .dll in CTypes in Python

import ctypes
from ctypes.util import find_library
from ctypes import *

path = os.path.dirname(os.path.realpath(__file__)) + '\\JuliaFunctions.dll'

# _lib = cdll.LoadLibrary(ctypes.util.find_library(path))  # same error
# hllDll = ctypes.WinDLL(path, winmode=0)  # same error
with os.add_dll_directory(os.path.dirname(os.path.realpath(__file__))):
    _lib = ctypes.CDLL(path, winmode=0)

I get OSError: [WinError 127] The specified procedure could not be found

With my current understanding, this means that CTypes found the dll and imported it, but didn't find.. something? I've yet to fully grasp how this behaves.

I've verified the function my_main_function is exported in the .dll file via Nirsoft's DLL Export Viewer. Users from previous similar issues have noted that this sysimage is already callable and should work, but they always add at the end something along the lines of "Note that you will also in general need to initialize the Julia runtime."

What does this mean? Is this even something that can be done independently from the Julia installation? The dev docs in PackageCompiler mention this, however they just mention that julia_main is automatically included in the .dll file and gets called as a sort of launch point. This function is also being exported correctly into the .dll file the above code creates. Below is an image of the Nirsoft export viewer output for reference.

Nirsoft .dll export viewer results

Edit 1 Inexplicably, I've rebuilt this .dll on another machine and made progress. Now, the dll is imported correctly. I'm not sure yet why this worked on a fresh Julia install + Python venv, but I'm going to reinstall them on the other one and update this if anything changes. For anyone encountering this, also note you need to specify the expected output, whatever it may be. In my case this is done by adding (after the import):

_lib.testmethod1.restype = c_double  # switched from Cfloat earlier, a lot has changed.
_lib.testmethod1.argtypes = [c_double, c_double]   # (defined by ctypes)

The current error is now OSError: exception: access violation writing 0x0000000000000024 when trying to actually use the function, which is specific to Python. Any help on this would also be appreciated.

Will
  • 170
  • 10
  • Hello, any luck with this ? – BambOo Apr 14 '21 at 07:23
  • Nope! Switched to pure Julia and started rewriting as the speed benefits and other library advantages (like `Measurements.jl`) were too good to ignore. Compiling standalone programs isn't fully mature yet in Julia but I believe they have it on their todo list. – Will Apr 15 '21 at 12:34
  • 1
    I have quite a similiar problem, with lots of computations in julia and a GUI in VB, but switching to full julia is not possible ... I opened some questions on the discourse if you are interested https://discourse.julialang.org/t/correct-process-to-use-julia-code-from-c-or-vb-net/58018 https://discourse.julialang.org/t/package-compiler-error-when-reproducing-documentation-example/58926. I got little succes so far, but at least I got some information about this PR https://github.com/JuliaLang/PackageCompiler.jl/pull/490 – BambOo Apr 15 '21 at 13:39
  • Thanks! You may want to consider how valuable your time is here, I found that I was sinking far more time into solving the strange issues that crop up than I would have been if I had learned a new programming language and did a full rewrite. Is there no possible way to switch at all? I also "abandoned" the gui I had written in Python, knowing that Julias GUI frameworks are currently immature as well. If I really need it, I plan on either calling python and having an installer package a compiled pyinstaller with it, or doing a web based GUI. – Will Apr 15 '21 at 14:00
  • The GUI is quite extensive, and would probably require half a year (at least) to re-create... However, this is an internal project with little redistribution, so the current developments to make julia binaries may help us achieve our goals – BambOo Apr 15 '21 at 14:33

0 Answers0