# How to evaluate Python custom code in PythonCall?

**URL:** <https://discourse.julialang.org/t/how-to-evaluate-python-custom-code-in-pythoncall/116113>\
**Category:** General Usage\
**Tags:** question, pycall, python, pythoncall\
**Created:** [June 23, 2024, 12:22pm UTC](https://discourse.julialang.org/t/how-to-evaluate-python-custom-code-in-pythoncall/116113 "2024-06-23T12:22:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [June 23, 2024, 12:22pm UTC](https://discourse.julialang.org/t/how-to-evaluate-python-custom-code-in-pythoncall/116113/1 "2024-06-23T12:22:33Z")

</div>

While I agree that most “call Python in Julia” refers to call existing libraries, I sometimes find it useful to be able to evaluate custom defined Python code from Julia.

Which is the PythonCall equivalent to this PyCall snippet ?

```julia
using PyCall
py"""
def sumMyArgs (i, j):
  return i+j
def getNElement (n):
  a = [0,1,2,3,4,5,6,7,8,9]
  return a[n]
"""

a = py"sumMyArgs"(3,4) # 7
b = py"sumMyArgs"([3,4],[5,6]) # [8,10]
c = py"sumMyArgs"([3,4],5) # [8,9]
d = py"getNElement"(1) # 1

```

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [June 23, 2024, 1:06pm UTC](https://discourse.julialang.org/t/how-to-evaluate-python-custom-code-in-pythoncall/116113/2 "2024-06-23T13:06:58Z")

</div>

See the related topic:

> [@Define Python function in Julia file with PythonCall?](https://discourse.julialang.org/t/define-python-function-in-julia-file-with-pythoncall/98593/7):
>
> Whenever you use pyexec you’re invoking the Python parser, which makes for an unfair comparison. It’s better to use it to create a Python function then call that: @pyexec """ def fib(n): a, b = 0, 1 while a \< n: print(a, end=' ') a, b = b, a+b print() """ =\> fib fib(10) This example uses @pyexec’s handy =\> notation for getting variables into and out of Python.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [June 23, 2024, 1:07pm UTC](https://discourse.julialang.org/t/how-to-evaluate-python-custom-code-in-pythoncall/116113/3 "2024-06-23T13:07:44Z")

</div>

yep… indeed, same need… thanks

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [June 23, 2024, 3:33pm UTC](https://discourse.julialang.org/t/how-to-evaluate-python-custom-code-in-pythoncall/116113/4 "2024-06-23T15:33:30Z")

</div>

This is a way to do:

```julia
using PythonCall, Pipe

i = 3
@pyexec (i=i, j=4) => """
a=i+j
b=i/j
""" => (a::Int64,b::Float64)
typeof(a)

@pyexec """
def python_sum(i, j):
    return i+j
""" => python_sum
@pyexec """
def get_ith_element(n):
    a = [0,1,2,3,4,5,6,7,8,9]
    return a[n]
""" => get_ith_element

c = @pipe python_sum(3,4) |> pyconvert(Int64,_) # 7
d = @pipe python_sum([3,4],[5,6]) |> pyconvert(Vector{Int64},_) # [8,10]
typeof(d) # Array{Int64,1}
e = @pipe get_ith_element(i) |> pyconvert(Int64,_) #3

```

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [June 23, 2024, 3:45pm UTC](https://discourse.julialang.org/t/how-to-evaluate-python-custom-code-in-pythoncall/116113/5 "2024-06-23T15:45:16Z")

</div>

How to import local custom python code library in Julia? I have a python file which i will need for running Julia code.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [June 23, 2024, 4:30pm UTC](https://discourse.julialang.org/t/how-to-evaluate-python-custom-code-in-pythoncall/116113/6 "2024-06-23T16:30:28Z")

</div>

Good question… I don’t have the answer, but the other way around is:

```python
>>> from juliacall import Main as jl
>>> jl.seval("include(\"julia_code.jl\")")

```

I am looking at PythonCall “seriously” for the first time, and while I like some aspects (one Python env for each Julia project is awesome) there are others much less nice… why don’t just let evaluate custom Python code in Python (like in PyCall py"“…”") instead of using lots of newly named functions ? Also, why `pyconvert` instead of just overriding `convert` ?

If you find the way, please post the answer here 😉

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [June 23, 2024, 5:11pm UTC](https://discourse.julialang.org/t/how-to-evaluate-python-custom-code-in-pythoncall/116113/7 "2024-06-23T17:11:08Z")

</div>

Here it is… …weird syntax…

Assume you have a `python_code.py` file with the following content:

```python
def python_sum(i, j):
    return i+j

def get_ith_element(n):
    a = [0,1,2,3,4,5,6,7,8,9]
    return a[n]

```

You can then “import” it and call the individual functions defined there with:

```julia
i = 3
pyexec(read("python_code.py", String),Main)
@pyexec (i=3, j=4) => "f = python_sum(i,j)" => (f::Float64)

```

I still need to understand how to convert a Julia object in a Python one, for example an Array… I know that when using np operations the conversion is done automatically (and possibly without copying, that’s the nice thing) but I may want to use also python functions that weren’t in the tought of the JuliaCall developers, so in Python I would like to end up with a Python object…

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [June 24, 2024, 10:23am UTC](https://discourse.julialang.org/t/how-to-evaluate-python-custom-code-in-pythoncall/116113/8 "2024-06-24T10:23:27Z")

</div>

I got how to convert the Julia object into a Python one… writing it here for reference…

When you call in Python a Julia function, the object you have is:

- for primitive, immutable scalars, the primitive equivalent of python
- for arrays and mutable types, a Python object that wrap the Julia one.

While this object, in the case of arrays, supports the Numpy interface, it may not be accepted by functions that want a “real” numpy array. In such circumstances it can be converted to “real” numpy array with `obj.to_numpy()`.
