# Trying PackageCompiler.jl lib example on Python

**URL:** <https://discourse.julialang.org/t/trying-packagecompiler-jl-lib-example-on-python/115492>\
**Category:** General Usage\
**Tags:** package-compiler\
**Created:** [June 11, 2024, 4:14pm UTC](https://discourse.julialang.org/t/trying-packagecompiler-jl-lib-example-on-python/115492 "2024-06-11T16:14:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![el-analista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/el-analista/32/209911_2.png) [@el-analista](https://discourse.julialang.org/u/el-analista)\
**Post date:** [June 11, 2024, 4:14pm UTC](https://discourse.julialang.org/t/trying-packagecompiler-jl-lib-example-on-python/115492/1 "2024-06-11T16:14:15Z")

</div>

Hey there,

I am testing PackageCompiler with the examples in the repo. I am able to compile the library correctly following the documentation but then when I try to use it in Python with the following code I just get

```julia
segmentation fault python test_lib.py

```

```julia
import ctypes
import os

print(os.getcwd())
mylib = ctypes.CDLL('MyLibCompiled/lib/libinc.so')
mylib.increment32.argtypes = [ctypes.c_int]
mylib.increment32.restype = ctypes.c_int
mylib.increment64.argtypes = [ctypes.c_long]
mylib.increment64.restype = ctypes.c_long
count32 = 10
result32 = mylib.increment32(count32)
print(f"Result of increment32: {result32}")

```

Debugging it, basically it crashes when calling the increment method.  
Wondering if anyone could point me to debug what is going on.  
Many thanks,  
Eduard

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [June 12, 2024, 1:33am UTC](https://discourse.julialang.org/t/trying-packagecompiler-jl-lib-example-on-python/115492/2 "2024-06-12T01:33:22Z")

</div>

You need to initilaize Julia:  
[https://docs.julialang.org/en/v1/manual/embedding/](https://docs.julialang.org/en/v1/manual/embedding/)

---

<div class="post-metadata">

**Author:** ![el-analista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/el-analista/32/209911_2.png) [@el-analista](https://discourse.julialang.org/u/el-analista)\
**Post date:** [June 12, 2024, 9:33am UTC](https://discourse.julialang.org/t/trying-packagecompiler-jl-lib-example-on-python/115492/3 "2024-06-12T09:33:48Z")

</div>

Good point!  
I am trying to initialize it with

```julia
import ctypes
import os
from ctypes import c_void_p, RTLD_GLOBAL

libjulia = ctypes.CDLL('MyLibCompiled/lib/libjulia.so', RTLD_GLOBAL)
libjulia.jl_init()

```

however when doing jl\_init() I get

```julia
ERROR: could not load library "MyLibCompiled/lib/../bin/../lib/julia/sys.so"
MyLibCompiled/lib/../bin/../lib/julia/sys.so: cannot open shared object file: No such file or directory

```

As the path is trimmed I cant really know what is happening but it seems is trying to find this file but it didnt get ocmpiled?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [June 12, 2024, 2:33pm UTC](https://discourse.julialang.org/t/trying-packagecompiler-jl-lib-example-on-python/115492/4 "2024-06-12T14:33:53Z")

</div>

So you need to set `jl_options` with the location of the system image you just compiled.

Frankly, you should probably use the Python package `juliacall` to at least initialize Julia, then try to call the C function pointer.

---

<div class="post-metadata">

**Author:** ![el-analista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/el-analista/32/209911_2.png) [@el-analista](https://discourse.julialang.org/u/el-analista)\
**Post date:** [June 13, 2024, 7:55pm UTC](https://discourse.julialang.org/t/trying-packagecompiler-jl-lib-example-on-python/115492/5 "2024-06-13T19:55:44Z")

</div>

Thanks again Mark! I would prefer to avoid using juliacall as I plan to call from other languages such as java. I found a libcg repo which successfully build and calls a librar From c and rust however i read that there are some libraries that don’t work well and that everything needs to be statically typed which limits the usage quite a bit 🙃

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [June 14, 2024, 2:33am UTC](https://discourse.julialang.org/t/trying-packagecompiler-jl-lib-example-on-python/115492/6 "2024-06-14T02:33:38Z")

</div>

This is slightly easier to describe from either C or Zig than Python.

You can see the `julia_init` function the libcg function that libcg uses works:

> <https://github.com/JuliaLang/PackageCompiler.jl/blob/master/src%2Fjulia_init.c>

Basically, we need to communicate to Julia the location of the system image. You can either populate the jl\_options struct directly or use the command line option processor. The `-J` or ` --sysimage` command line option indicates the system image location.

As I say this though, I suspect that the `juliac` tool is about to be reincarnated, which might provide a whole new pathway to do this.

The bottom line is that you must initialize the Julia runtime before invoking that C pointer.
