PyCall.jl icon indicating copy to clipboard operation
PyCall.jl copied to clipboard

TypeError("cannot pickle 'PyCall.jlwrap' object")

Open seadra opened this issue 2 years ago • 0 comments

Hi,

I'm trying to find a way to use the python package adaptive from Julia using PyCall, but I can't even get the simplest example working: https://adaptive.readthedocs.io/en/latest/tutorial/tutorial.Learner1D.html#tutorial-learner1d


using PyCall

adaptive = pyimport("adaptive")

f(x) = x*x
learner = adaptive.Learner1D(f, bounds=(-1, 1))
runner = adaptive.Runner(learner, goal=l -> l.loss() < 0.01 )

This gives the following error:

PyError ($(Expr(:escape, :(ccall(#= /home/user/.julia/packages/PyCall/L0fLP/src/pyfncall.jl:43 =# @pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, pyargsptr, kw))))) <class 'TypeError'>
TypeError("cannot pickle 'PyCall.jlwrap' object")
  File "/home/user/.local/lib/python3.10/site-packages/adaptive/runner.py", line 527, in __init__
    pickle.dumps(learner.function)


Stacktrace:
  [1] pyerr_check
    @ ~/.julia/packages/PyCall/L0fLP/src/exception.jl:62 [inlined]
  [2] pyerr_check
    @ ~/.julia/packages/PyCall/L0fLP/src/exception.jl:66 [inlined]
  [3] _handle_error(msg::String)
    @ PyCall ~/.julia/packages/PyCall/L0fLP/src/exception.jl:83
  [4] macro expansion
    @ ~/.julia/packages/PyCall/L0fLP/src/exception.jl:97 [inlined]
  [5] #107
    @ ~/.julia/packages/PyCall/L0fLP/src/pyfncall.jl:43 [inlined]
  [6] disable_sigint
    @ ./c.jl:458 [inlined]
  [7] __pycall!
    @ ~/.julia/packages/PyCall/L0fLP/src/pyfncall.jl:42 [inlined]
  [8] _pycall!(ret::PyObject, o::PyObject, args::Tuple{PyObject}, nargs::Int64, kw::PyObject)
    @ PyCall ~/.julia/packages/PyCall/L0fLP/src/pyfncall.jl:29
  [9] _pycall!
    @ ~/.julia/packages/PyCall/L0fLP/src/pyfncall.jl:11 [inlined]
 [10] (::PyObject)(args::PyObject; kwargs::Base.Pairs{Symbol, var"#7#8", Tuple{Symbol}, NamedTuple{(:goal,), Tuple{var"#7#8"}}})
    @ PyCall ~/.julia/packages/PyCall/L0fLP/src/pyfncall.jl:86
 [11] top-level scope
    @ In[15]:1
 [12] eval
    @ ./boot.jl:373 [inlined]
 [13] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
    @ Base ./loading.jl:1196

The python code in adaptive package throwing the error is this: https://github.com/python-adaptive/adaptive/blob/master/adaptive/runner.py#L527

So somehow, the adaptive package doesn't like the Julia function, because pickle.dump throws an error on it (is this a bug?). Are there any workarounds for using adaptive from Julia?

seadra avatar Mar 03 '22 04:03 seadra