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

Hi, community!

When a module is defined, such as in

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!

See GitHub - JuliaPackaging/Requires.jl: Lazy code loading for Julia

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.

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

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

    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
1 Like