# Storing and importing structure containing a PyObject

**URL:** <https://discourse.julialang.org/t/storing-and-importing-structure-containing-a-pyobject/105711>\
**Category:** Data\
**Created:** [November 2, 2023, 5:03pm UTC](https://discourse.julialang.org/t/storing-and-importing-structure-containing-a-pyobject/105711 "2023-11-02T17:03:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vivekag7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vivekag7/32/202869_2.png) [@vivekag7](https://discourse.julialang.org/u/vivekag7)\
**Post date:** [November 2, 2023, 5:03pm UTC](https://discourse.julialang.org/t/storing-and-importing-structure-containing-a-pyobject/105711/1 "2023-11-02T17:03:24Z")

</div>

So I want to store a structure called Results which contains the following:  
(:rgi\_id, :H, :S, :B, :V, :Vx, :Vy, :Δx, :Δy, :gdir)

Where

- :H, :S, :B, :V, :Vx, :Vy, are 2D matrices
- :rgi\_id, is a string
- :Δx, :Δy, are floats
- :gdir is a PyObject

The problem is if I save and load these results the PyObject becomes: PyObject NULL.  
Code that i used:

```julia
using JLD2
@save "/home/jovyan/vivekag7/Huginn_env/glacier2D_test_temporal.jld2" results

@load "/home/jovyan/vivekag7/Huginn_env/glacier2D_test_temporal.jld2" results

```

Does anyone know how to properly save and load this structure? Also for more information on what is contained in the object, see the following:

```julia
PyObject <oggm.GlacierDirectory>
  RGI id: RGI60-11.01450
  Region: 11: Central Europe
  Subregion: 11-01: Alps                            
  Glacier type: Glacier
  Terminus type: Land-terminating
  Status: Glacier or ice cap
  Area: 82.206 km2
  Lon, Lat: (8.01919, 46.5028)
  Grid (nx, ny): (107, 154)
  Grid (dx, dy): (137.0, -137.0)

```

---

<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:** [November 2, 2023, 7:02pm UTC](https://discourse.julialang.org/t/storing-and-importing-structure-containing-a-pyobject/105711/2 "2023-11-02T19:02:42Z")

</div>

> [@vivekag7](#):
>
> The problem is if I save and load these results the PyObject becomes: PyObject NULL.

Yes, you need something that knows how to serialize Python objects.

One option is to call something like [`pickle`](https://docs.python.org/3/library/pickle.html) manually before saving to JLD2. Another option is to use the Julia [Serialization](https://docs.julialang.org/en/v1/stdlib/Serialization/) standard library, since [PyCall supports the `serialize` interface](https://github.com/JuliaPy/PyCall.jl/blob/04c477858b84c1a2dfcf450d0250f3527b56035b/src/serialize.jl) (via `pickle`, actually).

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [November 2, 2023, 7:38pm UTC](https://discourse.julialang.org/t/storing-and-importing-structure-containing-a-pyobject/105711/3 "2023-11-02T19:38:25Z")

</div>

You can also add custom serialisation to JLD2 so that a Python object gets serialised via it’s pickle representation (for example).
