# Call julia function from python

**URL:** <https://discourse.julialang.org/t/call-julia-function-from-python/75202>\
**Category:** General Usage\
**Tags:** question, juliacall, pyjulia\
**Created:** [January 25, 2022, 9:00pm UTC](https://discourse.julialang.org/t/call-julia-function-from-python/75202 "2022-01-25T21:00:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![xspeng](https://avatars.discourse-cdn.com/v4/letter/x/90ced4/32.png) [@xspeng](https://discourse.julialang.org/u/xspeng)\
**Post date:** [January 25, 2022, 9:00pm UTC](https://discourse.julialang.org/t/call-julia-function-from-python/75202/1 "2022-01-25T21:00:07Z")

</div>

Hi, guys, I use bayesion optimization of python plus subfunction of julia to do my simulation, and I can successfully run the simple function see here, maybe I should ask another question, if I want to combine the speed of julia and the convenience of python, should I call julia sub-function(needs much time) from python or call python’s bayesian optimization packages from julia, I am in a mess…

```julia
# sub-function written in julia
function black_box_function(x, y) #save in the name of "example.jl"
    return -x^2 - (y - 1)^2 + 1
end

```

```julia
# run in python
from bayes_opt import BayesianOptimization
from julia import Main
Main.include("example.jl")
Main.black_box_function(2,3) # it works   

```

however if I put the sub-function into the bayesian optimization, then it crashed

```julia
# run in python
from bayes_opt import BayesianOptimization
from julia import Main
Main.include("example.jl")

# def black_box_function(x, y):
# return -x **2 - (y - 1)** 2 + 1 # initial sub-function defined in python, and works well

# Bounded region of parameter space
pbounds = {'x': (2, 4), 'y': (-3, 3)}

optimizer = BayesianOptimization(  
    f=Main.black_box_function, # sub-function defined in julia, doesn't work
    pbounds=pbounds,
    random_state=1,
)

optimizer.maximize(                       
    init_points=5,
    n_iter=5,
)

```

---

<div class="post-metadata">

**Author:** ![cchderrick](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@cchderrick](https://discourse.julialang.org/u/cchderrick)\
**Post date:** [January 25, 2022, 9:32pm UTC](https://discourse.julialang.org/t/call-julia-function-from-python/75202/2 "2022-01-25T21:32:16Z")

</div>

looks like your python function is passing keyword arguments instead of positional arguments.

[https://github.com/maffettone/bayes\_opt/blob/master/bayes\_opt/target\_space.py#L223-L225](https://github.com/maffettone/bayes_opt/blob/master/bayes_opt/target_space.py#L223-L225)

so in the Julia side, do one more definitions so it takes keyword arguments, hopefully that works:

```julia
black_box_function(;x, y) = black_box_function(x, y) 

```

---

<div class="post-metadata">

**Author:** ![xspeng](https://avatars.discourse-cdn.com/v4/letter/x/90ced4/32.png) [@xspeng](https://discourse.julialang.org/u/xspeng)\
**Post date:** [January 25, 2022, 9:48pm UTC](https://discourse.julialang.org/t/call-julia-function-from-python/75202/3 "2022-01-25T21:48:24Z")

</div>

Yes, it works, thanks
