# How to avoid repeatedly calling include()?

**URL:** <https://discourse.julialang.org/t/how-to-avoid-repeatedly-calling-include/54347>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [January 31, 2021, 10:28pm UTC](https://discourse.julialang.org/t/how-to-avoid-repeatedly-calling-include/54347 "2021-01-31T22:28:15Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![MattWillFlood](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattwillflood/32/20722_2.png) [@MattWillFlood](https://discourse.julialang.org/u/MattWillFlood)\
**Post date:** [January 31, 2021, 10:28pm UTC](https://discourse.julialang.org/t/how-to-avoid-repeatedly-calling-include/54347/1 "2021-01-31T22:28:15Z")

</div>

Hello Julia Community 🙂,

I’m developing my first Julia package at the moment. In the main module of this package, there are many functions that call functions from other modules. For example:

```julia
module main_module

function foo(; kwargs)
   include("Module_One.jl")
   x = Module_One.Function_One(kwargs...)
   return x
end

function bar(; kwargs)
   include("Module_Two.jl")
   y = Module_Two.Function_Two(kwargs...)
   return y
end

function baz(; kwargs...)
   include("Module_Three.jl")
   z = Module_Three.Function_Three(kwargs...)
   return z
end

```

Unless I’m mistaken 👀, if I call one of these functions regularly, e.g. foo(), then each time it will look to include Module\_One, which seems wasteful…

To be more efficient, would it be wise/foolish to do the following instead? 🤨

```julia
module main_module

function foo(; kwargs)
   (@isdefined Module_One) ? nothing : include("Module_One.jl")
   x = Module_One.Function_One(kwargs...)
   return x
end

```

The idea being to skip re-including “Module\_One” each time the function is called again…  
Or is Julia efficient enough already that doing this is unnecessary?

_Having tried to run code this way already, I tend to get a world age error the first time (i.e_. The applicable method may be too new: running in world age 12345 _) but it works ok after the first run…_

Any advice will be greatly appreciated 🙏🏼 😇  
Thanks!

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [January 31, 2021, 10:51pm UTC](https://discourse.julialang.org/t/how-to-avoid-repeatedly-calling-include/54347/2 "2021-01-31T22:51:07Z")

</div>

To take a step back.  
Why are you doing this?  
What is you actual high level goals?  
What do you think your code does?

This is incredibly atypical Julia code.  
It’s not clear to me if you think you are doing something standard.  
If if you are doing something atypical for a reason.  
Without knowing that it is hard to answer appropriately.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [January 31, 2021, 10:58pm UTC](https://discourse.julialang.org/t/how-to-avoid-repeatedly-calling-include/54347/3 "2021-01-31T22:58:48Z")

</div>

> [@MattWillFlood](#):
>
> _Having tried to run code this way already, I tend to get a world age error the first time (i.e_ . The applicable method may be too new: running in world age 12345 _) but it works ok after the first run…_

Since `include` dynamically creates functions in the global scope, those functions have a newer world age than the one for example `foo` is currently running in. You can make this work the first time by using `Base.invokelatest(Module_One.Function_One)` instead of `Module_One.Function_One()`, but I have to agree with @oxinabox that this is typically an antipattern in Julia.

---

<div class="post-metadata">

**Author:** ![MattWillFlood](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattwillflood/32/20722_2.png) [@MattWillFlood](https://discourse.julialang.org/u/MattWillFlood)\
**Post date:** [January 31, 2021, 11:16pm UTC](https://discourse.julialang.org/t/how-to-avoid-repeatedly-calling-include/54347/4 "2021-01-31T23:16:12Z")

</div>

Hi @oxinabox, thanks for your help 🙂 👍🏼

> [@oxinabox](#):
>
> To take a step back.  
> Why are you doing this?

In the example above, there are just three functions in main\_module (foo, bar and baz), but in reality there are \> 40. Each function has many lines of code, so putting many functions with lots of lines of code in main\_module was cumbersome and messy to work with.  
I want people to be able to simply call `main_module.foo(; kwargs)` or `main_module.bar(; kwargs)` etc., but have the code for each function stored in a different file (“`Module_One.jl`”, “`Module_Two.jl`”).

The nature of these functions means that they will be used in loops (likely \>10000 times). So my thinking was that after the first time `main_module.foo(; kwargs)` was called, the `@isdefined` if statement would avoid including `Module_One.Function_One` again and again.

> [@oxinabox](#):
>
> This is incredibly atypical Julia code.  
> It’s not clear to me if you think you are doing something standard.  
> If if you are doing something atypical for a reason.  
> Without knowing that it is hard to answer appropriately.

I appreciate that it’s atypical, but I couldn’t think of a simpler way to go about it… 🤷🏼‍♂️  
Hopefully my goals are clearer now 🤞🏼

---

<div class="post-metadata">

**Author:** ![MattWillFlood](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattwillflood/32/20722_2.png) [@MattWillFlood](https://discourse.julialang.org/u/MattWillFlood)\
**Post date:** [January 31, 2021, 11:20pm UTC](https://discourse.julialang.org/t/how-to-avoid-repeatedly-calling-include/54347/5 "2021-01-31T23:20:35Z")

</div>

Thanks @simeonschaub 👍🏼  
That’s a very clear explanation.

Hopefully my reply above might elucidate the issue further… 🤞🏼

---

<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:** [January 31, 2021, 11:38pm UTC](https://discourse.julialang.org/t/how-to-avoid-repeatedly-calling-include/54347/6 "2021-01-31T23:38:35Z")

</div>

My first question is why do you have a new module for each of these functions? Every of these `include("Module_One.jl")` declare a new module? Why? You know the code inside the files included by `include` do not need to be wrapped in a module, right?

My second question is why do you not do something like:

```julia
# MainModule file (please do not use snake_case for module names)
module MainModule
    include("foo.jl")
    include("bar.jl")
    include("baz.jl")
    ...
end

```

and then

```julia
# file foo.jl
function foo(; kwargs)
    # long body of your function here
end

```

---

<div class="post-metadata">

**Author:** ![patrick-kidger](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/patrick-kidger/32/20378_2.png) [@patrick-kidger](https://discourse.julialang.org/u/patrick-kidger)\
**Post date:** [February 1, 2021, 12:10am UTC](https://discourse.julialang.org/t/how-to-avoid-repeatedly-calling-include/54347/7 "2021-02-01T00:10:13Z")

</div>

So the usual pattern in Julia is to structure packages something like the following:

```julia
# MyModule.jl
module MyModule
export foo_functionality_one, foo_functionality_two, bar_functionality

include("foo_file.jl")
include("bar_file.jl")
end

# foo_file.jl
foo_functionality_one() = ...
foo_functionality_two() = ...
foo_functionality_private() = ...

# bar_file.jl
function bar_functionality()
    something = foo_functionality_private()
    ...
end

```

~~Each `include` literally just copy-pastes the file’s contents in where you call it.~~ ( **EDIT: not quite accurate, see oxinabox’s comment below.** ) So you should usually put the `include`s outside the functions, not inside.

You do need to make sure that you don’t `include` the same file twice, as else everything gets evaluated twice, and you end up with two versions of the same function floating around.

If there’s dependencies between `foo_file.jl` and `bar_file.jl`, as in the example above, this means that you just implicitly assume that they’re present in `bar_file.jl`, and rely on `MyModule.jl` including the necessary functionality.

Likewise it’s up to you to get the `include`s in the right order – if for example `bar_file.jl` needs something in `foo_file.jl` to be evaluated first.

With that out of the way, you may like [FromFile](https://github.com/Roger-luo/FromFile.jl), which handles the above details for you. This means you can write the above example package as

```julia
# MyModule.jl
module MyModule
using FromFile
export foo_functionality_one, foo_functionality_two, bar_functionality

@from "foo_file.jl" import foo_functionality_one, foo_functionality_two
@from "bar_file.jl" import bar_functionality
end

# foo_file.jl
foo_functionality_one() = ...
foo_functionality_two() = ...
foo_functionality_private() = ...

# bar_file.jl
using FromFile
@from "foo_file.jl" import foo_functionality_private

function bar_functionality()
    something = foo_functionality_private()
    ...
end

```

i.e. making dependencies explicit, handling duplicate calls, not having to worry about `include` order.

Does that help you get started?

---

<div class="post-metadata">

**Author:** ![owinter92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/owinter92/32/21346_2.png) [@owinter92](https://discourse.julialang.org/u/owinter92)\
**Post date:** [February 1, 2021, 10:10am UTC](https://discourse.julialang.org/t/how-to-avoid-repeatedly-calling-include/54347/8 "2021-02-01T10:10:10Z")

</div>

Maybe, you could try to use Julia’s multiple dispatch capabilities together with types. This is very simplified example, not sure what exactly you want to achieve.

```julia
struct Foo1
    name
end

struct Foo2
    name
end

function run(v::Foo1)
    println("Foo1")
end

function run(v::Foo2)
    println("Foo2")
end

run(Foo1("a"))
run(Foo2("a"))

```

and results

```julia
Foo1
Foo2

```

You could generalize it with aid of abstract type/types.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [February 1, 2021, 10:21am UTC](https://discourse.julialang.org/t/how-to-avoid-repeatedly-calling-include/54347/9 "2021-02-01T10:21:40Z")

</div>

Also, you may have a look on my modules/packages tutorial for a broader discussion…

> **[11 - Developing Julia packages](https://syl1.gitbook.io/julia-language-a-concise-tutorial/language-core/11-developing-julia-packages)**

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [February 1, 2021, 10:28am UTC](https://discourse.julialang.org/t/how-to-avoid-repeatedly-calling-include/54347/10 "2021-02-01T10:28:18Z")

</div>

> [@patrick-kidger](#):
>
> Each `include` literally just copy-pastes the file’s contents in where you call it.

To be clear it doesn’t actually do this.  
Which is **why** putting it in a function is odd.  
Because the contents won’t end up inside the function.

`include` evaluates the contents of the file **at global scope**

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [February 1, 2021, 12:39pm UTC](https://discourse.julialang.org/t/how-to-avoid-repeatedly-calling-include/54347/11 "2021-02-01T12:39:28Z")

</div>

I remember that I thought as well that `include` just copy-pastes. This misunderstanding does come up repeatedly on this forum. Probably calling it `eval_file` or something alike would have been clearer.

---

<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:** [February 1, 2021, 12:44pm UTC](https://discourse.julialang.org/t/how-to-avoid-repeatedly-calling-include/54347/12 "2021-02-01T12:44:13Z")

</div>

This misunderstanding is, in fact, so common that I made [a half-phrase PR](https://github.com/JuliaLang/julia/pull/39317) just to change the documentation from:

> " `include` behaves as if the contents of the source file were evaluated in its place."

to

> " `include` behaves as if the contents of the source file were evaluated in the global scope of the including module."

---

<div class="post-metadata">

**Author:** ![airpmb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/airpmb/32/7826_2.png) [@airpmb](https://discourse.julialang.org/u/airpmb)\
**Post date:** [February 1, 2021, 10:51pm UTC](https://discourse.julialang.org/t/how-to-avoid-repeatedly-calling-include/54347/13 "2021-02-01T22:51:45Z")

</div>

I’m sure that half of the confusion is people with C/C++ experience mapping Julia ‘include’ to C ‘#include’ in their minds. I know that was a factor for me.

At least in Julia we don’t have the subtlety of angle brackets vs double quotes.

Anyway I wonder, why is include() even valid to write anywhere other than in module-global scope?

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [February 1, 2021, 11:01pm UTC](https://discourse.julialang.org/t/how-to-avoid-repeatedly-calling-include/54347/14 "2021-02-01T23:01:50Z")

</div>

> [@airpmb](#):
>
> Anyway I wonder, why is include() even valid to write anywhere other than in module-global scope?

It’s a fair question. 2 answers

1. Because it is actually just a normal function (that does some normal things like reading a file, and then `ccall`ing a very unnormal function). Normal functions can occur anywhere in julia code
2. Occationally it is useful to conditionally define a bunch of things at global scope. See how **Requires.jl** is used inside ` __init__ ` functions

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 1, 2021, 11:31pm UTC](https://discourse.julialang.org/t/how-to-avoid-repeatedly-calling-include/54347/15 "2021-02-01T23:31:31Z")

</div>

Maybe it is even worth reinforcing that if a function includes a file and is executed, and later the included file is modified, the function will execute the new code. That is, the include inside the function is a call to include, not at all a copy and paste. (I think that a note about that would be nice in the manual).

```julia
julia> function f()
         include("./file.jl")
       end
f (generic function with 1 method)

julia> @code_lowered f()
CodeInfo(
1 ─ %1 = Main.include("./file.jl")
└── return %1
)

```

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [February 1, 2021, 11:58pm UTC](https://discourse.julialang.org/t/how-to-avoid-repeatedly-calling-include/54347/16 "2021-02-01T23:58:09Z")

</div>

> [@mauro3](#):
>
> I remember that I thought as well that `include` just copy-pastes. This misunderstanding does come up repeatedly on this forum. Probably calling it `eval_file` or something alike would have been clearer.

Agreed. I had exactly this thought some time ago …

> [@Include mystery](https://discourse.julialang.org/t/include-mystery/22360/12):
>
> I must confess to being tripped up by misunderstanding exactly what include does. Indeed, I think it is a common misconception. I can’t help but feel that it is partly due to the name include which might be associated with pre-processor directives in other languages. And it is commonly used for the same or similar functionality. For me the name eval\_file would help remind me that it is a normal function executed at run time and which evals the contents of the file (in global scope since it is…

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 2, 2021, 12:36am UTC](https://discourse.julialang.org/t/how-to-avoid-repeatedly-calling-include/54347/17 "2021-02-02T00:36:35Z")

</div>

I wander how many packages would be broken if the behavior of `include` suddenly became an actual copy-paste.

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [February 2, 2021, 1:36am UTC](https://discourse.julialang.org/t/how-to-avoid-repeatedly-calling-include/54347/18 "2021-02-02T01:36:01Z")

</div>

Almost every package that calls include in it’s ` __init__ `, or that depends on one that does.

A lower bound on this is every package with a transitive dependency on Requires.jl  
Which is 1304 packages

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [February 2, 2021, 1:49am UTC](https://discourse.julialang.org/t/how-to-avoid-repeatedly-calling-include/54347/19 "2021-02-02T01:49:07Z")

</div>

See [Including code that defines a module within a function definition does not fail!? · Issue #39421 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/39421)

---

<div class="post-metadata">

**Author:** ![MattWillFlood](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattwillflood/32/20722_2.png) [@MattWillFlood](https://discourse.julialang.org/u/MattWillFlood)\
**Post date:** [February 6, 2021, 8:49pm UTC](https://discourse.julialang.org/t/how-to-avoid-repeatedly-calling-include/54347/20 "2021-02-06T20:49:21Z")

</div>

Thanks for all the comments on this 👍🏼. It seems that the implementation of `include()` is commonly misinterpreted 🤷🏼‍♂️

Knowing that `include()` brings contents of files into the **global** scope, I can see how my original idea is redundant (and somewhat faulty if the same functions are included separately -

> [@patrick-kidger](#):
>
> You do need to make sure that you don’t `include` the same file twice, as else everything gets evaluated twice, and you end up with two versions of the same function floating around.

).  
It might be best just to include all the functions that _may_ be called at the top of `main_module`, even though 90% of the them likely won’t be used. 🤨

Just out of curiosity, is there a way to call functions/code from another file locally within a function? 🤔 🤔

Thanks again guys! 🙌🏼 🙌🏼

[Next page](https://discourse.julialang.org/t/how-to-avoid-repeatedly-calling-include/54347.md?page=2)
