__init__() doesn't allow access to pycall functions

I am running a test on the implementation of init() for pycall and I built an example using the scipy.interpolate.Rbf and PyCall documentation.

module test_rbf

using PyCall

function __init__()
    import scipy.interpolate as interp
    def rad_fit(a, b, c):
        return interp.Rbf(a, b, c, function='multiquadric', smooth=0.5)

    def rad_pred(radial, a, b):
        return radial(a, b)

function rbf_fit(x, y, z) 
    return py"rad_fit"(x, y, z) 

function rbf_predict(rbfi, x, y) 
    return py"rad_pred"(rbfi, x, y)

x = rand(20)
y = rand(20)
z = rand(20)

x_pred = rand(20)
y_pred = rand(20)

rbfi = rbf_fit(x,y,z)
z = rbf_pred(rbfi, x_pred, y_pred)
println("z is $z")

However, this code returns an error saying the function rbf_fit() from init() is not defined.
Stackrace for the error:

LoadError: PyError ($(Expr(:escape, :(ccall(#= C:\Users\saads\.julia\packages\PyCall\BD546\src\pyeval.jl:38 =# @pysym(:PyEval_EvalCode), PyPtr, (PyPtr, 
PyPtr, PyPtr), o, globals, locals))))) <class 'NameError'>
NameError("name 'rad_fit' is not defined")
  File "C:\Users\user\.julia\packages\PyCall\BD546\src\pyeval.jl", line 1, in <module>
    const Py_single_input = 256  # from Python.h

 [1] pyerr_check
   @ ~\.julia\packages\PyCall\BD546\src\exception.jl:62 [inlined]
 [2] pyerr_check
   @ ~\.julia\packages\PyCall\BD546\src\exception.jl:66 [inlined]
 [3] _handle_error(msg::String)
   @ PyCall ~\.julia\packages\PyCall\BD546\src\exception.jl:83
in expression starting at c:\Users\user\Desktop\test\test_2.jl:1

May I know what could be the cause of this issue.
Thanks!! Look forward to the responses and suggestion!!

All top-level code inside the module is executed before __init__ is called, so you can’t call rbf_fit and rbf_pred there before the module is properly loaded. You probably want to put that inside a function and call it from outside the module. Alternatively, you could also just put all of that code inside the __init__ function.


Thank you for the response @simeonschaub, highly appreciate it!!
I did some testing using your suggestions, since i am working with micro-service architecture, putting it is a separate module helped. But haven’t ran a benchmarking yet. Probably will test the performance on the whole operation for both methods!!