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

**URL:** <https://discourse.julialang.org/t/calling-a-julia-function-from-python-pass-a-dictionary-of-arguments-and-get-an-array-returned/32052>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 9, 2019, 3:45pm UTC](https://discourse.julialang.org/t/calling-a-julia-function-from-python-pass-a-dictionary-of-arguments-and-get-an-array-returned/32052 "2019-12-09T15:45:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![00krishna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00krishna/32/8843_2.png) [@00krishna](https://discourse.julialang.org/u/00krishna)\
**Post date:** [December 9, 2019, 3:45pm UTC](https://discourse.julialang.org/t/calling-a-julia-function-from-python-pass-a-dictionary-of-arguments-and-get-an-array-returned/32052/1 "2019-12-09T15:45:31Z")

</div>

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.  
[https://stackoverflow.com/questions/49750067/running-julia-jl-file-in-python](https://stackoverflow.com/questions/49750067/running-julia-jl-file-in-python)

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.

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

```

And then a corresponding python file to call it.

```julia
#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.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 9, 2019, 3:57pm UTC](https://discourse.julialang.org/t/calling-a-julia-function-from-python-pass-a-dictionary-of-arguments-and-get-an-array-returned/32052/2 "2019-12-09T15:57:41Z")

</div>

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

```nohighlight
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:

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

```

---

<div class="post-metadata">

**Author:** ![00krishna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00krishna/32/8843_2.png) [@00krishna](https://discourse.julialang.org/u/00krishna)\
**Post date:** [December 10, 2019, 5:10am UTC](https://discourse.julialang.org/t/calling-a-julia-function-from-python-pass-a-dictionary-of-arguments-and-get-an-array-returned/32052/3 "2019-12-10T05:10:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![J\_Friedman](https://avatars.discourse-cdn.com/v4/letter/j/7993a0/32.png) [@J\_Friedman](https://discourse.julialang.org/u/J_Friedman)\
**Post date:** [December 15, 2020, 2:23am UTC](https://discourse.julialang.org/t/calling-a-julia-function-from-python-pass-a-dictionary-of-arguments-and-get-an-array-returned/32052/4 "2020-12-15T02:23:52Z")

</div>

> [@stevengj](#):
>
> `a = Main.foo(mydict)`

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(c)  
print(d)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 15, 2020, 2:41am UTC](https://discourse.julialang.org/t/calling-a-julia-function-from-python-pass-a-dictionary-of-arguments-and-get-an-array-returned/32052/5 "2020-12-15T02:41:53Z")

</div>

> [@J\_Friedman](#):
>
> However it takes a few minutes to run this tiny program. Is there a way to speed up the execution?

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).
