# How to patch another package?

**URL:** https://discourse.julialang.org/t/how-to-patch-another-package/113454
**Category:** General Usage
**Tags:** question
**Created:** [April 24, 2024, 2:38pm UTC](https://discourse.julialang.org/t/how-to-patch-another-package/113454 "2024-04-24T14:38:52Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)
#### Post date: [April 24, 2024, 2:38pm UTC](https://discourse.julialang.org/t/how-to-patch-another-package/113454/1 "2024-04-24T14:38:52Z")

</div>

Hi there,

I would like to patch another package. This worked perfectly fine up to julia 1.9 but with 1.10 it broke. See this thread [ERROR: Method overwriting is not permitted during Module precompilation. Use ` __precompile__ (false)` to opt-out of precompilation](https://discourse.julialang.org/t/error-method-overwriting-is-not-permitted-during-module-precompilation-use-precompile-false-to-opt-out-of-precompilation/109191)

Concretely I would like to patch PythonCall’s `serialize_py` and `deserialize_py` methods to use `dill` instead of `pickle`.

It seems the only option is to make the package ` __precompile__ (false)`, which is just a bit too much for me. The alternative sometimes suggested is to use ` __init__ `, but this does not work because [`@eval` is buggy inside ` __init__ `](https://github.com/JuliaLang/julia/issues/39648), aka does not work, and you also aren’t allowed to define top-level functions inside ` __init__ `.

This is very crucial for the project I am working on. Any help is highly appreciated.

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [April 24, 2024, 2:42pm UTC](https://discourse.julialang.org/t/how-to-patch-another-package/113454/2 "2024-04-24T14:42:28Z")

</div>

Fork and use your own fork?

---

<div class="post-metadata">

### Author: ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)
#### Post date: [April 24, 2024, 2:45pm UTC](https://discourse.julialang.org/t/how-to-patch-another-package/113454/3 "2024-04-24T14:45:04Z")

</div>

I was not thinking about this - yes, sounds like a workaround.

I still would prefer to stay on top of the original package for easier updates.

---

<div class="post-metadata">

### Author: ![cpfiffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpfiffer/32/208747_2.png) [@cpfiffer](https://discourse.julialang.org/u/cpfiffer)
#### Post date: [April 24, 2024, 6:57pm UTC](https://discourse.julialang.org/t/how-to-patch-another-package/113454/4 "2024-04-24T18:57:50Z")

</div>

Can you contribute something to the underlying package (a keyword argument or something) that lets you choose the format?

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [April 24, 2024, 8:48pm UTC](https://discourse.julialang.org/t/how-to-patch-another-package/113454/5 "2024-04-24T20:48:19Z")

</div>

> [@schlichtanders](#):
>
> It seems the only option is to make the package ` __precompile__ (false)`, which is just a bit too much for me.

Why is it “too much”? Overwriting a method works reliably in Julia, packages with the overwrite just cannot be precompiled.

And btw, even without this ` __precompile__ ` line, everything should just work! A scary error is printed, but the methods get overwritten properly. Not sure why it is printed as “error” instead of info or warning…

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [April 24, 2024, 11:05pm UTC](https://discourse.julialang.org/t/how-to-patch-another-package/113454/6 "2024-04-24T23:05:36Z")

</div>

The new function `Base.generating_output()` in [Julia 1.11](https://github.com/JuliaLang/julia/pull/51216/files) will help with this. `Base.generating_output()` is `true` if generating a pkgimage cache. You do not watn to perform patching then.

```julia
if !Base.generating_output()
    # hacky patching
end

```

If Julia 1.10, you can define it as follows:

```julia
generating_output() = ccall(:jl_generating_output, Cint, ()) != 0

```

---

<div class="post-metadata">

### Author: ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)
#### Post date: [April 25, 2024, 7:34am UTC](https://discourse.julialang.org/t/how-to-patch-another-package/113454/7 "2024-04-25T07:34:28Z")

</div>

` __precompile__ (false)` regards the entire package as far as I understand it, well as all packages dependening on it, which results in bad user experience as the package time to first anything is quite slow

---

<div class="post-metadata">

### Author: ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)
#### Post date: [April 25, 2024, 7:37am UTC](https://discourse.julialang.org/t/how-to-patch-another-package/113454/8 "2024-04-25T07:37:10Z")

</div>

Thank you, this sounds interesting. Would this `if !Base.generating_output()` block be executed?

I mean is it like ` __init__ ` in that we have a guarantee that it will run? Or is it more like a normal `if` in the file where depending one the if clause on part is executed and the other maybe never.

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [April 25, 2024, 12:54pm UTC](https://discourse.julialang.org/t/how-to-patch-another-package/113454/9 "2024-04-25T12:54:37Z")

</div>

Julia is “generating output” when it is “precompiling”. This helps us distinguish between the precompiling state and other states on module loading.
