# JuliaCall: is it possible to pass objects from Python to Julia Main?

**URL:** https://discourse.julialang.org/t/juliacall-is-it-possible-to-pass-objects-from-python-to-julia-main/88156
**Category:** General Usage
**Tags:** question, juliacall
**Created:** [October 3, 2022, 1:14pm UTC](https://discourse.julialang.org/t/juliacall-is-it-possible-to-pass-objects-from-python-to-julia-main/88156 "2022-10-03T13:14:34Z")
**Posts on this page:** 3
**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: [October 3, 2022, 1:14pm UTC](https://discourse.julialang.org/t/juliacall-is-it-possible-to-pass-objects-from-python-to-julia-main/88156/1 "2022-10-03T13:14:34Z")

</div>

Using JuliaCall, is it possible to pass objects from Python to Julia so that I can then use them in evaluated Julia code ?

E.g.:

```julia
>>> from juliacall import Main as jl
>>> xpy = [1,2,3]
>>> jl.xj = xpy # AttributeError: 'ModuleValue' object has no attribute 'xj'
>>> s = jl.seval("sum(xj)") # this is where I want to arrive

```

Of course, I am aware I can simply do in this case:

```julia
>>> jl.sum(xpy)

```

but I wonder if I can explicitly transfer (possibly without manually using `convert`) objects from Python to Julia.

---

<div class="post-metadata">

### Author: ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)
#### Post date: [October 3, 2022, 3:32pm UTC](https://discourse.julialang.org/t/juliacall-is-it-possible-to-pass-objects-from-python-to-julia-main/88156/2 "2022-10-03T15:32:25Z")

</div>

It’s admittedly not clear (I’ve now fixed this) but if you read down the full error stack the underlying error is:

```nohighlight
AttributeError: Julia: cannot assign variables in other modules

```

which means that you can only assign a variable to a Julia module from code executing inside the module - this is a general Julia restriction.

If you really want you can write a function which uses `@eval` to do it:

```python
>>> jlstore = jl.seval("(k, v) -> (@eval $(Symbol(k)) = $v; return)")
>>> jlstore("foo", [1,2,3])
>>> jl.foo
[1, 2, 3]

```

---

<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: [October 3, 2022, 4:11pm UTC](https://discourse.julialang.org/t/juliacall-is-it-possible-to-pass-objects-from-python-to-julia-main/88156/3 "2022-10-03T16:11:52Z")

</div>

Yes, it works:

```julia
>>> from juliacall import Main as jl
>>> xpy = [1,2,3]
>>> jlstore = jl.seval("(k, v) -> (@eval $(Symbol(k)) = $v; return)")
>>> jlstore("xj", xpy)
>>> s = jl.seval("sum(xj)")
>>> s
6

```

I think it would be useful to have this function in JuliaCall. Both PyJulia and R JuliaCall have something similar:

```julia
>>> jl = julia.Julia(compiled_modules=False)
>>> import julia
>>> from julia import Main
>>> xpy = [1,2,3]
>>> Main.xj = xpy
>>> jl.eval("sum(xj)")
6

```

```julia
> library(JuliaCall)
> xr = c(1,2,3)
> julia_assign("xj",xr)
> julia_eval("sum(xj)")
[1] 6
> 

```
