# \_\_init\_\_() doesn't allow access to pycall functions

**URL:** <https://discourse.julialang.org/t/init-doesnt-allow-access-to-pycall-functions/67402>\
**Category:** General Usage\
**Tags:** question, pycall\
**Created:** [August 31, 2021, 5:11am UTC](https://discourse.julialang.org/t/init-doesnt-allow-access-to-pycall-functions/67402 "2021-08-31T05:11:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdsa3d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mdsa3d/32/20389_2.png) [@mdsa3d](https://discourse.julialang.org/u/mdsa3d)\
**Post date:** [August 31, 2021, 5:11am UTC](https://discourse.julialang.org/t/init-doesnt-allow-access-to-pycall-functions/67402/1 "2021-08-31T05:11:47Z")

</div>

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.  
**Code:**

```julia
module test_rbf

using PyCall

function __init__ ()
    py"""
    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)
    """
end

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

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

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")
end

```

However, this code returns an error saying the function rbf\_fit() from _init_() is not defined.  
_Stackrace for the error:_

```julia
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

Stacktrace:
 [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!!

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [August 31, 2021, 9:27am UTC](https://discourse.julialang.org/t/init-doesnt-allow-access-to-pycall-functions/67402/2 "2021-08-31T09:27:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mdsa3d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mdsa3d/32/20389_2.png) [@mdsa3d](https://discourse.julialang.org/u/mdsa3d)\
**Post date:** [September 6, 2021, 6:12am UTC](https://discourse.julialang.org/t/init-doesnt-allow-access-to-pycall-functions/67402/3 "2021-09-06T06:12:21Z")

</div>

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!!
