Calling a Julia function from Python--pass a dictionary of arguments and get an array returned

I am writing a little package in julia, but I also need to plug the package into an existing python jupyter notebook, etc.

So I was hoping someone could tell me how to call a Julia function from Python 3. That is, I want to call a Julia function, and pass that function a dictionary of arguments. Then I want the function to run and return an array of results. Is that possible in Julia and Python?

In looking around I saw this StackOverflow post, which was helpful.

The user created a Julia file and called it from python. BUT the example does not show how to pass arguments, nor does it show if I can return some sort of array structure, or at least some pointer to an array.

#juliafile.jl
for i in 1:10
    println(i)
end
1+2

And then a corresponding python file to call it.

#pythonfile.py
>>> import julia
>>> j = julia.Julia()
>>> x = j.include("test.jl")
1
2
3
4
5
6
7
8
9
10
>>> x
3

Any suggestions. Thanks.

1 Like

Usually Julia functions are defined in Julia modules. If you have a Julia module Foo that defines a function foo you want to call, just do

from julia import Foo
a = Foo.foo(mydict)

Passing dictionaries, arrays, etcetera should all work bidirectionally.

If your function is defined in a foo.jl file, I believe you’ll have to include the file and then import it from the Main module:

import julia
j = julia.Julia()
j.include("foo.jl")
from julia import Main
a = Main.foo(mydict)
3 Likes

Okay great. @stevengj this does indeed seem to work. That is excellent. Thank you so much. I had to make the adjustment of j = julia.Julia(compiled_modules=False), but otherwise this worked great.

1 Like

I essentially followed the directions above. However it takes a few minutes to run this tiny program. Is there a way to speed up the execution? Can I pre-compile the julia module somehow. Any suggestions. See my code below.

#############################
Here is my julia module
using JuMP, Gurobi, Cbc, Random, GLPK

module Jsec
export createSec
export testInterface
end

function testInterface(tup)
md = tup[1]
a = tup[2]
for s in keys(md)
println(s)
end
println(a)

C = Array{Int64,1}(1:10)
D = capacityC = Dict{Int64,Int64}()
D[1]=Int64(7)
D[9]=Int64(8)
return (C,D)

end

Python calling program
######################
import julia
j = julia.Julia(compiled_modules=False)
from julia import Main
Main.include(“jsecMod.jl”)

d = {}
d[1]=2
d[2]=5
a = 7
mytup = (d,a)
(c,d) = Main.testInterface(mytup)
print©
print(d)

1 Like

Launching Julia, and loading Julia modules, is currently somewhat slow — look elsewhere for many discussions of the “time to first plot” problem. This is improving with each new version of Julia, and should continue to improve in the future (especially once Julia caches compiled code).

In the meantime, Julia is mainly used for large calculations (where the initial startup time is irrelevant) or for interactive use (where one typically launches a Julia session, e.g. in Jupyter, and leaves it running for a long time over the course of many interactions).

3 Likes