# An idea for putting imports inside of Julia functions

**URL:** https://discourse.julialang.org/t/an-idea-for-putting-imports-inside-of-julia-functions/78840
**Category:** General Usage
**Created:** [March 31, 2022, 10:58pm UTC](https://discourse.julialang.org/t/an-idea-for-putting-imports-inside-of-julia-functions/78840 "2022-03-31T22:58:01Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)
#### Post date: [March 31, 2022, 10:58pm UTC](https://discourse.julialang.org/t/an-idea-for-putting-imports-inside-of-julia-functions/78840/1 "2022-03-31T22:58:01Z")

</div>

I’ve often wanted to put imports inside of functions to delay loading potentially slow-to-load packages until I _really_ need them in a given session (a habit from Python, but maybe even more useful in Julia due to latency). The other day I put together this idea which I figured I’d share for feedback or if anyone else finds it useful. Definitely hacky so beware, but the concept is maybe ok I think.

The idea is it lets you write stuff like:

```julia
function foo()
    @dynamic import Images
    Images.bilinear_interpolation(rand(10,10), 2.5, 3.7)
end

```

and `Images` (as an example here) won’t be loaded until `foo` is actually called (you still need `Images` it in your Project.toml). You can put `foo` in a precompiled package and its fine too.

Note that it wouldn’t work if you simply did `@eval import Images` because of world-age, which is the main thing this macro works around, by essentially redirecting to an `invokelatest(foo)` after `@eval`’ing the import. As a consequence it breaks inference, but that’s to be expected for anything like this.

Code is: [https://gist.github.com/marius311/22ee06a6e8223da77ee60bb44f3ba1c6](https://gist.github.com/marius311/22ee06a6e8223da77ee60bb44f3ba1c6)

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [April 1, 2022, 5:51am UTC](https://discourse.julialang.org/t/an-idea-for-putting-imports-inside-of-julia-functions/78840/2 "2022-04-01T05:51:51Z")

</div>

Is it similar to [https://github.com/JuliaPackaging/Requires.jl](https://github.com/JuliaPackaging/Requires.jl)?

---

<div class="post-metadata">

### Author: ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)
#### Post date: [April 1, 2022, 6:13am UTC](https://discourse.julialang.org/t/an-idea-for-putting-imports-inside-of-julia-functions/78840/3 "2022-04-01T06:13:37Z")

</div>

Slightly different purpose I’d say. With Requires.jl, you can do stuff in response to the user loading a package. With this, you can trigger the package load yourself.
