# NULL PyObject in package

**URL:** <https://discourse.julialang.org/t/null-pyobject-in-package/96461>\
**Category:** Package Management\
**Tags:** question, package, pycall, error, error-message\
**Created:** [March 22, 2023, 4:46pm UTC](https://discourse.julialang.org/t/null-pyobject-in-package/96461 "2023-03-22T16:46:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![asorlik](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@asorlik](https://discourse.julialang.org/u/asorlik)\
**Post date:** [March 22, 2023, 4:46pm UTC](https://discourse.julialang.org/t/null-pyobject-in-package/96461/1 "2023-03-22T16:46:38Z")

</div>

I am creating a package and I have gotten the following error in one of my scripts that is called via include(“script\_name.jl”):

```julia
ERROR: ArgumentError: ref of NULL PyObject

```

The error is coming from a usage of np.gradient() within a function.

Oddly this function works perfectly well in a julia notebook, and also I was able to use np.gradient in the package by adding “println(np.gradient([…])” to the start of the script, which printed during compilation of my package.

My issue may have something to do with ([Calling the constructor outside a module works, but inside a module throws ArgumentError: ref of NULL PyObject - #2 by Volker](https://discourse.julialang.org/t/calling-the-constructor-outside-a-module-works-but-inside-a-module-throws-argumenterror-ref-of-null-pyobject/42362/2)) but I don’t know julia well enough to understand the solution provided.

For reference I have the following code at the beginning of the script

```julia
using PyCall
np = pyimport("numpy")

```

I also have these same lines in another script that uses python functions, but this doesn’t seem to be causing an issue.

Also, I tried to find a pure julia equivalent for np.gradient of arrays and there does not seem to be one. Does anyone know if there is such an implementation in julia?

Thank you!

---

<div class="post-metadata">

**Author:** ![asorlik](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@asorlik](https://discourse.julialang.org/u/asorlik)\
**Post date:** [March 22, 2023, 7:53pm UTC](https://discourse.julialang.org/t/null-pyobject-in-package/96461/2 "2023-03-22T19:53:26Z")

</div>

I was able to solve the problem, but I do not understand the solution entirely. In my script I have changed np = pyimport(“numpy”) to

```julia
using PyCall

function __init__ ()
    py"""
    import numpy as np

    def grad(A, dx):
        return np.gradient(A, dx)
    """
end

function test()
    println(py"grad"([1,2,3,4], 0.5))
end

```

and I get the correct functionality from the test method. Why is it that, when using SciPy I can call “SciPy.interpolate.interp1d” with no issues from within functions of my package, but “np.gradient” is not recognized unless I modify the **init** method?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 22, 2023, 7:59pm UTC](https://discourse.julialang.org/t/null-pyobject-in-package/96461/3 "2023-03-22T19:59:29Z")

</div>

> [@asorlik](#):
>
> `function __init__ ()`

If you are using a module, see [Using PyCall from Julia Modules](https://github.com/JuliaPy/PyCall.jl#using-pycall-from-julia-modules)
