# Call python packages from Julia

**URL:** <https://discourse.julialang.org/t/call-python-packages-from-julia/75324>\
**Category:** General Usage\
**Tags:** question, pycall\
**Created:** [January 27, 2022, 9:27pm UTC](https://discourse.julialang.org/t/call-python-packages-from-julia/75324 "2022-01-27T21:27:23Z")\
**Posts on this page:** 4\
**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 27, 2022, 9:27pm UTC](https://discourse.julialang.org/t/call-python-packages-from-julia/75324/1 "2022-01-27T21:27:23Z")

</div>

Hi, with the help of PyCall.jl, I can easily run a script of python, but if I want to replace the function defined in python with the function defined in julia, it reminds me cannot find the sub-function, can anybody help me to have a look, because in the future, I want to use quite complex sub-function written in julia, here is a simple example

```julia

function black_box_function(x, y) # with this function, doesn't work
    return -x^2 - (y - 1)^2 + 1
end

using PyCall

py"""
from bayes_opt import BayesianOptimization

#def black_box_function(x, y): # with this function, it works well
    #return -x **2 - (y - 1)** 2 + 1

pbounds = {'x': (2, 4), 'y': (-3, 3)}

optimizer = BayesianOptimization(
    f=black_box_function,
    pbounds=pbounds,
    random_state=1,
)

optimizer.maximize(
    init_points=2,
    n_iter=3,
)

print(optimizer.max)

"""

```

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [January 27, 2022, 9:59pm UTC](https://discourse.julialang.org/t/call-python-packages-from-julia/75324/2 "2022-01-27T21:59:01Z")

</div>

You need to “interpolate” the variable to send it to the Python side, something like `f=$black_box_function`, assuming you defined `black_box_function` in Julia as a function.

Fwiw I believe there are Bayesian optimization packages for Julia as well that might be interesting to check out.

---

<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 27, 2022, 10:13pm UTC](https://discourse.julialang.org/t/call-python-packages-from-julia/75324/3 "2022-01-27T22:13:02Z")

</div>

Yeah. I checked julia already, but it’s still under developing, that’s why I go for python

---

<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 28, 2022, 11:03am UTC](https://discourse.julialang.org/t/call-python-packages-from-julia/75324/4 "2022-01-28T11:03:26Z")

</div>

Hi, I find a way that works, hope that can give some helps for other people, here is my code

```julia

function black_box_function(;x, y) # here the ';' is because of the difference of parameters transfer, see this please[https://discourse.julialang.org/t/call-julia-function-from-python/75202](https://discourse.julialang.org/t/call-julia-function-from-python/75202)

    return -x^2 - (y - 1)^2 + 1
end

using PyCall

py"""
pbounds = {'x': (2, 4), 'y': (-3, 3)}
"""
temp=pyimport("bayes_opt")

optimizer = temp.BayesianOptimization(
    f=black_box_function,
    pbounds=py"pbounds",
    random_state=1,
)

optimizer.maximize(
    init_points=2,
    n_iter=3,
)

print(optimizer.max)

```
