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

**URL:** <https://discourse.julialang.org/t/calling-the-constructor-outside-a-module-works-but-inside-a-module-throws-argumenterror-ref-of-null-pyobject/42362>\
**Category:** General Usage\
**Tags:** package, pycall\
**Created:** [July 1, 2020, 12:10pm UTC](https://discourse.julialang.org/t/calling-the-constructor-outside-a-module-works-but-inside-a-module-throws-argumenterror-ref-of-null-pyobject/42362 "2020-07-01T12:10:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Volker](https://avatars.discourse-cdn.com/v4/letter/v/77aa72/32.png) [@Volker](https://discourse.julialang.org/u/Volker)\
**Post date:** [July 1, 2020, 12:10pm UTC](https://discourse.julialang.org/t/calling-the-constructor-outside-a-module-works-but-inside-a-module-throws-argumenterror-ref-of-null-pyobject/42362/1 "2020-07-01T12:10:53Z")

</div>

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:

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

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

```

---

<div class="post-metadata">

**Author:** ![Volker](https://avatars.discourse-cdn.com/v4/letter/v/77aa72/32.png) [@Volker](https://discourse.julialang.org/u/Volker)\
**Post date:** [July 2, 2020, 11:39am UTC](https://discourse.julialang.org/t/calling-the-constructor-outside-a-module-works-but-inside-a-module-throws-argumenterror-ref-of-null-pyobject/42362/2 "2020-07-02T11:39:27Z")

</div>

@stevengj Do you maybe have a clue?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [July 2, 2020, 11:59am UTC](https://discourse.julialang.org/t/calling-the-constructor-outside-a-module-works-but-inside-a-module-throws-argumenterror-ref-of-null-pyobject/42362/3 "2020-07-02T11:59:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Volker](https://avatars.discourse-cdn.com/v4/letter/v/77aa72/32.png) [@Volker](https://discourse.julialang.org/u/Volker)\
**Post date:** [July 2, 2020, 12:06pm UTC](https://discourse.julialang.org/t/calling-the-constructor-outside-a-module-works-but-inside-a-module-throws-argumenterror-ref-of-null-pyobject/42362/4 "2020-07-02T12:06:52Z")

</div>

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](https://github.com/JuliaPy/PyCall.jl)

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

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

```
