# How to delay the load of dependencies in a module to "using" time?

**URL:** <https://discourse.julialang.org/t/how-to-delay-the-load-of-dependencies-in-a-module-to-using-time/90914>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 27, 2022, 7:28pm UTC](https://discourse.julialang.org/t/how-to-delay-the-load-of-dependencies-in-a-module-to-using-time/90914 "2022-11-27T19:28:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jgidi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jgidi/32/26206_2.png) [@jgidi](https://discourse.julialang.org/u/jgidi)\
**Post date:** [November 27, 2022, 7:28pm UTC](https://discourse.julialang.org/t/how-to-delay-the-load-of-dependencies-in-a-module-to-using-time/90914/1 "2022-11-27T19:28:54Z")

</div>

Hi, community!

When a module is defined, such as in

```julia
module MyModule
using PyCall
end

```

the module PyCall will be loaded, even if you haven’t run `using .MyModule`.

The main question is: Is it possible to delay this load of dependencies to the time when you run `using MyModule`? and if possible, Is there an apparent reason for this to be a bad idea?

Thanks in advance!

---

<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:** [November 27, 2022, 7:39pm UTC](https://discourse.julialang.org/t/how-to-delay-the-load-of-dependencies-in-a-module-to-using-time/90914/2 "2022-11-27T19:39:09Z")

</div>

See [GitHub - JuliaPackaging/Requires.jl: Lazy code loading for Julia](https://github.com/JuliaPackaging/Requires.jl)

---

<div class="post-metadata">

**Author:** ![jgidi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jgidi/32/26206_2.png) [@jgidi](https://discourse.julialang.org/u/jgidi)\
**Post date:** [November 27, 2022, 8:15pm UTC](https://discourse.julialang.org/t/how-to-delay-the-load-of-dependencies-in-a-module-to-using-time/90914/3 "2022-11-27T20:15:43Z")

</div>

Thanks @mkitti for your eager response, but I think that the package provided does not cover the use case I’m looking for.

I understand that Requires allows to trigger the execution of portions of code (such as `using (...)` ) after some other package is also loaded. What I want is to run portions of code after the module itself is loaded, which is subtly different, and (I think) is not covered.

---

<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:** [November 27, 2022, 8:32pm UTC](https://discourse.julialang.org/t/how-to-delay-the-load-of-dependencies-in-a-module-to-using-time/90914/4 "2022-11-27T20:32:47Z")

</div>

You could either use requires and then trigger loading via `@eval using MyLazyModule` or maybe the later is sufficient.

---

<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:** [November 27, 2022, 8:38pm UTC](https://discourse.julialang.org/t/how-to-delay-the-load-of-dependencies-in-a-module-to-using-time/90914/5 "2022-11-27T20:38:19Z")

</div>

You may want to take a close look at how Requires.jl works and examine thr consequences for precompilation and static compilation.

> <https://github.com/JuliaPackaging/Requires.jl/blob/master/src/require.jl#L86-L109>

```julia
    if !isprecompiling() 
       listenpkg($pkg) do 
         withpath($srcfile) do 
           err($ __module__ , $modname, $srcfile, $srcline) do 
             $(esc(:(eval($(Expr(:quote, Expr(:block, 
                                             :(const $(Symbol(modname)) = Base.require($pkg)), 
                                             expr))))))) 
           end 
         end 
         $withnotifications($srcfile, $ __module__ , $idstr, $modname, $(esc(Expr(:quote, expr)))) 
       end 
     end

```
