"Packaging" dependencies into a sysimage

Based on the tutorial of PackageCompiler.jl, I am using the following to create a sysimage: julia --project=. _create_sysimage.jl. That file contains:

_create_sysimage.jl:

import Pkg
Pkg.add("PackageCompiler")
Pkg.add("Example")

import PackageCompiler

PackageCompiler.create_sysimage(
    ["Example"];
    sysimage_path="ExampleSysimagePrecompile.so",
    precompile_execution_file="_precompile_execution.jl"
)

and

_precompile_execution.jl:

import Example
Example.hello("somestring")

If I now try to use that sysimage, by calling

julia -JExampleSysimagePrecompile.so -e 'import Example; Example.hello("friend")'

it does not find Example:

ERROR: ArgumentError: Package Example not found in current path, maybe you meant `import/using .Example`.

Now I thought the sysimage to be a way to “package” the current state of my Julia session - which would include the package Example, that I am also explicitly passing to the packages argument of create_sysimage(...) - which apparently is not the case.

Running

julia --project=. -JExampleSysimagePrecompile.so -e 'import Example; Example.hello("friend")'

instead (which loads the temporary project, that Pkg.add(...) installed Example into), works. But why doesn’t the first one work too?

I think doing import .Example should work, and actually the binding Example is already present when you load your sysimage, so importing is superfluous.

2 Likes

That makes a lot of sense, and that’s the main thing I was missing (I kinda just stuck to the Manual, and didn’t really think that through).

Facing the same confusion - is there really no way we can use plain import Example (without the dot?)

Motivation: It would for sure simplify the code if we could use the same import statements when not using sysimage and when using sysimage

AFAIU you can use a plain import Example (without the dot), but then you also need to activate the project that lists Example in its dependencies.

2 Likes