# Is there a command for forcing a fresh precompilation of a module?

**URL:** <https://discourse.julialang.org/t/is-there-a-command-for-forcing-a-fresh-precompilation-of-a-module/49730>\
**Category:** General Usage\
**Created:** [November 7, 2020, 11:15am UTC](https://discourse.julialang.org/t/is-there-a-command-for-forcing-a-fresh-precompilation-of-a-module/49730 "2020-11-07T11:15:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wg030](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wg030/32/12944_2.png) [@wg030](https://discourse.julialang.org/u/wg030)\
**Post date:** [November 7, 2020, 11:15am UTC](https://discourse.julialang.org/t/is-there-a-command-for-forcing-a-fresh-precompilation-of-a-module/49730/1 "2020-11-07T11:15:06Z")

</div>

Hey, I am wondering if one can force a module to be precompiled. So I am looking for a command like

```julia
precompilemodule( "pathto/MyModule.jl")

```

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [November 7, 2020, 12:31pm UTC](https://discourse.julialang.org/t/is-there-a-command-for-forcing-a-fresh-precompilation-of-a-module/49730/2 "2020-11-07T12:31:39Z")

</div>

It’s easiest to [create a package](https://github.com/invenia/PkgTemplates.jl) and `dev` it.

---

<div class="post-metadata">

**Author:** ![wg030](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wg030/32/12944_2.png) [@wg030](https://discourse.julialang.org/u/wg030)\
**Post date:** [November 7, 2020, 2:34pm UTC](https://discourse.julialang.org/t/is-there-a-command-for-forcing-a-fresh-precompilation-of-a-module/49730/3 "2020-11-07T14:34:59Z")

</div>

Thanks for your answer @tim.holy

But it does not look as if this was the best solution for my problem, maybe I should have described it a little more. So let me do it now:  
What I have is a module `MyModule` that collects all constants for a project that are used in different other modules of the project. However the problem is that some constants are loaded in `MyModule` from some `datafile.jld2` files using the `JLD2` package and those data might be updated from time to time.  
It took me a long time to figure that a precompalation of `MyModule` is required, when the data in `datafile.jld2` is updated. But the problem is, a precompalation will not automatically take place the next time the package is used because `MyModule.jl` actually never needs to be modified and (as far as I know) only modifications in `MyModule.jl` lead to new automatic precompalation.  
My module looks as follows:

```julia
module MyModule

using JLD2, FileIO

export somedata

@load "../jld2s/datafile.jld2" somedata

end

```

What I do right now is making some unecessary alibi modifcations in `MyModule.jl` to force a new precompalation when the data in `datafile.jld2` is updated. However from time to time I forget to do so and get into trouble.  
Hence what I would really love to do is forcing a precompalation immeditately after the data in `datafile.jld2` is updated.

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [November 8, 2020, 4:29pm UTC](https://discourse.julialang.org/t/is-there-a-command-for-forcing-a-fresh-precompilation-of-a-module/49730/4 "2020-11-08T16:29:15Z")

</div>

You can do this but `MyModule` will have to be a little more complicated. First, define an ` __init__ ` function and use `watch_file` from the `FileWatching` standard library so that you’re notified of changes to any files that you draw constants from. Second, your “constants” probably deserve a little bit of thought. If they are `const` then you will likely encounter trouble because they may get compiled into some of your methods that use them, and those won’t update. It would be better to have them be non-constant, and it might be worth considering storing them in `Dict` or something. You could then have your file-watching utility just reload the relevant items in the `Dict` when a file changes.

---

<div class="post-metadata">

**Author:** ![wg030](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wg030/32/12944_2.png) [@wg030](https://discourse.julialang.org/u/wg030)\
**Post date:** [November 16, 2020, 10:11am UTC](https://discourse.julialang.org/t/is-there-a-command-for-forcing-a-fresh-precompilation-of-a-module/49730/5 "2020-11-16T10:11:00Z")

</div>

Thanks for you answer, @tim.holy.  
I tried to use `watch_file`, but unfortuantely it did not work out as expected. I seemed as if `MyModule` was kind of waiting for changes in `datafile.jld2` and would not continue until the file was changed, but all changes take place before `MyModule` is used and hence it should check if `datafile.jld2` has changed instead of waiting for a change.  
However I’m quite greateful for your input and your idea anyway since it brought me on the right track. A solution that works as desired can be implemented by a single line of code, using the [`Base.include_dependency`](https://docs.julialang.org/en/v1/base/base/#Base.include_dependency) function.  
One would simply need to write

```julia
module MyModule

using JLD2, FileIO

export somedata

include_dependency( "../jld2s/datafile.jld2")
@load "../jld2s/datafile.jld2" somedata

end

```

This has the effect that `MyModule` will check if `datafile.jld2` has changed since the last precompaliation and recompile again if that’s the case, exactly as I want it to be.

I will now consider to store my data in a `Dict` as you suggested, too, because I think it makes sense.  
Thanks for your help!

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [November 16, 2020, 10:21am UTC](https://discourse.julialang.org/t/is-there-a-command-for-forcing-a-fresh-precompilation-of-a-module/49730/6 "2020-11-16T10:21:28Z")

</div>

Glad you found a good solution!

> [@wg030](#):
>
> waiting for changes in `datafile.jld2` and would not continue until the file was changed

That’s how `watch_file` works. Typically you use it in conjunction with an `@async` somewhere, using a separate `Task` to perform the updates. But as warned, this solution is more complicated, and if you don’t need the values to update mid-session then indeed `include_dependency` will be a much simpler solution.
