Calling the constructor outside a module works, but inside a module throws ArgumentError: ref of NULL PyObject

Hi,

I call the constructor of an custom python package, which I import with pyimport. Somehow it works when I do it in a script, but if I put the code into a module, it will throw an error “ArgumentError: ref of NULL PyObject”. Has anybody an idea what could be the reason?

Script:

DataPreProPython = pyimport("damo.modeling.dataprepro")
KerasIdentification = pyimport("damo.modeling.keras_identification")
data = DataPreProPython.DataPrePro(data_train, data_type="time_series", normalize_flag=false, data_val=data_val, num_overlap=N_overlap)

keras_identification = KerasIdentification.KerasIdentification(data, num_neurons, layer_types, list_activations, num_epochs,
                                           patience, file_path,
                                           verbose, optimizer, learning_rate, batch_size)
keras_identification.train()

Module:

module TestModulePyCall

using PyCall

DataPreProPython = pyimport("damo.modeling.dataprepro")
KerasIdentification = pyimport("damo.modeling.keras_identification")
function train(data_train, data_val, N_overlap, num_neurons, layer_types, list_activations, num_epochs,
                                           patience, file_path,
                                           verbose, optimizer, learning_rate, batch_size)

data = DataPreProPython.DataPrePro(data_train, data_type="time_series", normalize_flag=false, data_val=data_val, num_overlap=N_overlap)

keras_identification = KerasIdentification.KerasIdentification(data, num_neurons, layer_types, list_activations, num_epochs,
                                               patience, file_path,
                                               verbose, optimizer, learning_rate, batch_size)

    keras_identification.train()
end

end  # module TestModulePyCall

@stevengj Do you maybe have a clue?

The python object has to be initialized at module load time. See PyCall.__init__

2 Likes

ah ok. I thought that this is just necessary for functions, classes, etc. in py"“” … “”" and not work classes and packages, which are imported via pyimport. thanks a lot

Example of PyCall https://github.com/JuliaPy/PyCall.jl

__precompile__() # this module is safe to precompile
module MyModule
using PyCall

const scipy_opt = PyNULL()

function __init__()
    copy!(scipy_opt, pyimport_conda("scipy.optimize", "scipy"))
end

end

For my case:

__precompile__() # this module is safe to precompile
module TestModulePyCall

using PyCall

const DataPreProPython = PyNULL()
const KerasIdentification = PyNULL()

function __init__()
    copy!(DataPreProPython, pyimport("damo.modeling.dataprepro"))
    copy!(KerasIdentification,pyimport("damo.modeling.keras_identification"))
end

function train(data_train, data_val, N_overlap, num_neurons, layer_types, list_activations, num_epochs,
                                           patience, file_path,
                                           verbose, optimizer, learning_rate, batch_size)

    data = DataPreProPython.DataPrePro(data_train, data_type="time_series", normalize_flag=false, data_val=data_val, num_overlap=N_overlap)

    keras_identification = KerasIdentification.KerasIdentification(data, num_neurons, layer_types, list_activations, num_epochs,
                                               patience, file_path,
                                               verbose, optimizer, learning_rate, batch_size)

    keras_identification.train()
end

end  # module TestModulePyCall
3 Likes