# Save model- what do the Pickle as Python?

**URL:** <https://discourse.julialang.org/t/save-model-what-do-the-pickle-as-python/79644>\
**Category:** New to Julia\
**Created:** [April 18, 2022, 6:48pm UTC](https://discourse.julialang.org/t/save-model-what-do-the-pickle-as-python/79644 "2022-04-18T18:48:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![HosseinF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hosseinf/32/35059_2.png) [@HosseinF](https://discourse.julialang.org/u/HosseinF)\
**Post date:** [April 18, 2022, 6:48pm UTC](https://discourse.julialang.org/t/save-model-what-do-the-pickle-as-python/79644/1 "2022-04-18T18:48:24Z")

</div>

Hi,  
I am trying to save the model and results in StocasticPrograms. I tried `write_to_file(sp, "Models/model_1.mps")` but I faced an error:  
`NaNERROR: LoadError: MethodError: no method matching write_to_file(::StochasticProgram`

If it was in Python I just Pickle the model. How should I do in Julia?

Many thanks!

---

<div class="post-metadata">

**Author:** ![awasserman](https://avatars.discourse-cdn.com/v4/letter/a/9de0a6/32.png) [@awasserman](https://discourse.julialang.org/u/awasserman)\
**Post date:** [April 18, 2022, 8:10pm UTC](https://discourse.julialang.org/t/save-model-what-do-the-pickle-as-python/79644/2 "2022-04-18T20:10:31Z")

</div>

The [Serialization](https://docs.julialang.org/en/v1/stdlib/Serialization/) standard library package is probably what you’re looking for.

```julia
using Serialization
my_data = Dict(:a => 1, :b => [1, 2, 3])
serialize("my_data.jls", my_data)
my_data_from_serialized = deserialize("my_data.jls")
@assert my_data == my_data_from_serialized

```

Also of note, if the object you want to serialize has any custom data types contained within it, you’ll want to load up the modules with those data types prior to deserializing them.

---

<div class="post-metadata">

**Author:** ![HosseinF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hosseinf/32/35059_2.png) [@HosseinF](https://discourse.julialang.org/u/HosseinF)\
**Post date:** [April 19, 2022, 5:38am UTC](https://discourse.julialang.org/t/save-model-what-do-the-pickle-as-python/79644/3 "2022-04-19T05:38:11Z")

</div>

Thanks!  
It works!

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [April 19, 2022, 8:41am UTC](https://discourse.julialang.org/t/save-model-what-do-the-pickle-as-python/79644/4 "2022-04-19T08:41:39Z")

</div>

Be aware that the Serialization package is (analogue to Python Pickle) not intended for long-term storage - their main use case are inter-process communication.  
Especially it is not guaranteed that you can still access your data after upgrading either Julia itself or any used package.  
An alternative is

> **[GitHub - JuliaIO/JLD.jl: Saving and loading julia variables while preserving...](https://github.com/JuliaIO/JLD.jl)**
>
> Saving and loading julia variables while preserving native types - GitHub - JuliaIO/JLD.jl: Saving and loading julia variables while preserving native types

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [April 19, 2022, 10:56am UTC](https://discourse.julialang.org/t/save-model-what-do-the-pickle-as-python/79644/5 "2022-04-19T10:56:56Z")

</div>

> [@lungben](#):
>
> Especially it is not guaranteed that you can still access your data after upgrading either Julia itself or any used package.

We do actually promise that all future 1.x releases will be able to read serialized data from previous releases since [RFC: document compatibility properties of `serialize` by JeffBezanson · Pull Request #41520 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/41520). It doesn’t work the other way however, you can’t read data serialized with a more recent release from an older one.

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [April 19, 2022, 11:02am UTC](https://discourse.julialang.org/t/save-model-what-do-the-pickle-as-python/79644/6 "2022-04-19T11:02:07Z")

</div>

Good to know!

This part of the documentation is then outdated?

[https://docs.julialang.org/en/v1/stdlib/Serialization/](https://docs.julialang.org/en/v1/stdlib/Serialization/)

> `serialize(stream::IO, value)`  
> …  
> In general, this process will not work if the reading and writing are done by different versions of Julia, or an instance of Julia with a different system image.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [April 19, 2022, 12:22pm UTC](https://discourse.julialang.org/t/save-model-what-do-the-pickle-as-python/79644/7 "2022-04-19T12:22:55Z")

</div>

I believe that PR was merged after 1.7 was already branches, so it didn’t update the 1.7 docs. Perhaps it’s worth backporting though
