# Passing a dict from Python to Julia

**URL:** https://discourse.julialang.org/t/passing-a-dict-from-python-to-julia/116914
**Category:** General Usage
**Tags:** question, package, pythoncall
**Created:** [July 11, 2024, 2:28pm UTC](https://discourse.julialang.org/t/passing-a-dict-from-python-to-julia/116914 "2024-07-11T14:28:30Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [July 11, 2024, 2:28pm UTC](https://discourse.julialang.org/t/passing-a-dict-from-python-to-julia/116914/1 "2024-07-11T14:28:30Z")

</div>

How can I pass a Python dict to Julia using PythonCall?

This is not working:

```julia
from juliacall import Main as jl, convert as jlconvert

setdict = {
  "log_level": 1,
  "area": 20.0,
  "solver": "IDA"
}

jlstore = jl.seval("(k, v) -> (@eval $(Symbol(k)) = $v; return)")
jlstore("set", setdict)

print(jl.seval(set))

```

Error message:

```julia
In [1]: %run test_dict2struct.py
Detected IPython. Loading juliacall extension. See https://juliapy.github.io/PythonCall.jl/stable/compat/#IPython
  Activating project at `~/repos/pykitesim`
---------------------------------------------------------------------------
JuliaError Traceback (most recent call last)
~/repos/pykitesim/test_dict2struct.py in <module>
     15 jlstore("set", setdict)
     16 
---> 17 print(jl.seval(set))

~/repos/pykitesim/.pixi/envs/default/lib/python3.8/site-packages/juliacall/ __init__.py in seval(self, expr)
     23 return ValueBase. __dir__ (self) + self._jl_callmethod($(pyjl_methodnum(pyjlmodule_dir)))
     24 def seval(self, expr):
---> 25 return self._jl_callmethod($(pyjl_methodnum(pyjlmodule_seval)), expr)
     26 """, @ __FILE__ (), "exec"), jl. __dict__ )
     27 pycopy!(pyjlmoduletype, jl.ModuleValue)

JuliaError: cannot convert this Python 'type' to a Julia 'String'
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:35
 [2] macro expansion
   @ ~/.julia/packages/PythonCall/S5MOg/src/Convert/pyconvert.jl:357 [inlined]
 [3] macro expansion
   @ ~/.julia/packages/PythonCall/S5MOg/src/Core/Py.jl:132 [inlined]
 [4] pyconvert(::Type{String}, x::Py)
   @ PythonCall.Convert ~/.julia/packages/PythonCall/S5MOg/src/Convert/pyconvert.jl:372
 [5] pyjlmodule_seval(self::Module, expr::Py)
   @ PythonCall.JlWrap ~/.julia/packages/PythonCall/S5MOg/src/JlWrap/module.jl:13
 [6] _pyjl_callmethod(f::Any, self_::Ptr{PythonCall.C.PyObject}, args_::Ptr{PythonCall.C.PyObject}, nargs::Int64)
   @ PythonCall.JlWrap ~/.julia/packages/PythonCall/S5MOg/src/JlWrap/base.jl:66
 [7] _pyjl_callmethod(o::Ptr{PythonCall.C.PyObject}, args::Ptr{PythonCall.C.PyObject})
   @ PythonCall.JlWrap.Cjl ~/.julia/packages/PythonCall/S5MOg/src/JlWrap/C.jl:63

```

---

<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: [July 11, 2024, 3:41pm UTC](https://discourse.julialang.org/t/passing-a-dict-from-python-to-julia/116914/2 "2024-07-11T15:41:40Z")

</div>

> [@ufechner7](#):
>
> `print(jl.seval(set))`

You meant `"set"` (the string) not `set` (the type).

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [July 11, 2024, 5:38pm UTC](https://discourse.julialang.org/t/passing-a-dict-from-python-to-julia/116914/3 "2024-07-11T17:38:01Z")

</div>

This works:

```julia
from juliacall import Main as jl, convert as jlconvert

setdict = {
  "log_level": 1,
  "area": 20.0,
  "solver": "IDA"
}

jlstore = jl.seval("(k, v) -> (@eval $(Symbol(k)) = $v; return)")
jlstore("set", setdict)

print(jl.set)

```

Most of the time you should not use `jl.seval`, but sometimes it is needed. Not so clear to me when it is needed.
