# Load a dependency only when calling a method that needs it

**URL:** <https://discourse.julialang.org/t/load-a-dependency-only-when-calling-a-method-that-needs-it/48804>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 22, 2020, 7:44am UTC](https://discourse.julialang.org/t/load-a-dependency-only-when-calling-a-method-that-needs-it/48804 "2020-10-22T07:44:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [October 22, 2020, 7:44am UTC](https://discourse.julialang.org/t/load-a-dependency-only-when-calling-a-method-that-needs-it/48804/1 "2020-10-22T07:44:06Z")

</div>

I’m building a package for which I want to minimize `using` load time. It has a bunch of dependencies that are only needed for some specific functionalities, called via specific methods. Can I somehow delay loading the dependencies until the user calls the methods that use them? What is the recommended approach to such a problem?

Note: as I understand it, this is kind of the conjugate of the problem that Requires.jl solves (defining some methods only after the user loads a package).

---

<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:** [October 22, 2020, 7:55am UTC](https://discourse.julialang.org/t/load-a-dependency-only-when-calling-a-method-that-needs-it/48804/2 "2020-10-22T07:55:21Z")

</div>

You can put using in a function like this:

```julia
function load_dep()
    @eval using Dependency
end

```

But this works only if you call this function from global scope, if you call it inside another function, you will not get this dependency in the calling function due to world age.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 22, 2020, 12:13pm UTC](https://discourse.julialang.org/t/load-a-dependency-only-when-calling-a-method-that-needs-it/48804/3 "2020-10-22T12:13:29Z")

</div>

> [@pablosanjose](#):
>
> Can I somehow delay loading the dependencies until the user calls the methods that use them? What is the recommended approach to such a problem?

Put those methods in different packages with the relevant dependencies. But not that it may not save a lot of load time in any case.

Also, is load time a bottleneck for you on 1.5/master? For most people, compilation time is more significant.

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [October 22, 2020, 12:20pm UTC](https://discourse.julialang.org/t/load-a-dependency-only-when-calling-a-method-that-needs-it/48804/4 "2020-10-22T12:20:04Z")

</div>

You can use `@eval Base. __toplevel__ import $pkg` for loading and `Base.invokelatest(<function>, <args>)` for actual using. This is how it is done in `UrlDownload.jl`: [https://github.com/Arkoniak/UrlDownload.jl/blob/master/src/UrlDownload.jl#L117-L138](https://github.com/Arkoniak/UrlDownload.jl/blob/master/src/UrlDownload.jl#L117-L138) and this implementation was taken from [FileIO.jl](https://github.com/JuliaIO/FileIO.jl)

I am not sure, but I think that `invokelatest` can be rather slow compared to a normal function call.

---

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [October 22, 2020, 12:33pm UTC](https://discourse.julialang.org/t/load-a-dependency-only-when-calling-a-method-that-needs-it/48804/5 "2020-10-22T12:33:59Z")

</div>

Hey Tamas, yes, I’m usually developing on 1.5 and the package is already at 2 sec load time, and growing. I just tried on master and… 0.7 sec. What!!?

I guess I shouldn’t worry so much about dependencies going forward 😃

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [October 22, 2020, 2:13pm UTC](https://discourse.julialang.org/t/load-a-dependency-only-when-calling-a-method-that-needs-it/48804/6 "2020-10-22T14:13:27Z")

</div>

The reduction in the load time you observed in master may be an effect of [the work being done to reduce invalidations](https://julialang.org/blog/2020/08/invalidations/).

---

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [October 22, 2020, 3:04pm UTC](https://discourse.julialang.org/t/load-a-dependency-only-when-calling-a-method-that-needs-it/48804/7 "2020-10-22T15:04:21Z")

</div>

Yeah, I know, I’ve been following that closely with much interest. It’s amazing. I just never thought to measure its effect on my work 🙂
