Imported ScikitLearn PyObj becomes NULL in a module

module TestMod
using ScikitLearn: @sk_import

@sk_import linear_model: LogisticRegression

println(LogisticRegression)

export fun

function fun()
    println(LogisticRegression)
end

end

When the above TestMod is imported, LogisticRegressionis imported and printed:

PyObject <class ‘sklearn.linear_model.logistic.LogisticRegression’>

But after that, strangely LogisticRegression becomes NULL:

julia> TestMod.LogisticRegression
PyObject NULL

julia> TestMod.fun()
PyObject NULL

Yeah that’s tricky, you need to place the import in the init function, see how I did it here
https://github.com/baggepinnen/HDBSCAN.jl/blob/master/src/HDBSCAN.jl

1 Like

@baggepinnen, it works very well! I don’t know what to say. Thank you so much! :slight_smile:

Here is some small questions:

  1. what benefit would the use of @eval give us? On the surface, all is fine without @eval.
  2. How does the trick work?

You’ll find more info here
https://github.com/JuliaPy/PyCall.jl/blob/master/README.md#using-pycall-from-julia-modules
It seems there is a recommended approach slightly different from mine.

3 Likes

Aha, I understood what is happening now. Thank you!