# Using \`include\` to bring variables into scope not working

**URL:** <https://discourse.julialang.org/t/using-include-to-bring-variables-into-scope-not-working/46636>\
**Category:** Data\
**Created:** [September 15, 2020, 12:32pm UTC](https://discourse.julialang.org/t/using-include-to-bring-variables-into-scope-not-working/46636 "2020-09-15T12:32:02Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![gobs](https://avatars.discourse-cdn.com/v4/letter/g/bc8723/32.png) [@gobs](https://discourse.julialang.org/u/gobs)\
**Post date:** [September 15, 2020, 12:32pm UTC](https://discourse.julialang.org/t/using-include-to-bring-variables-into-scope-not-working/46636/1 "2020-09-15T12:32:02Z")

</div>

My workflow is to usually have a `init.jl` file which brings some variables into scope, e.g. `saveDir = "/path"`. Then all my scripts start with `include("init.jl")`. However, I noticed that this wasn’t always working, so I ended up doing `global saveDir = "/path"` (I know this is terrible practice, but hence the question).

I just tried to prove this to myself with a small example, but wasn’t able to get it to work. I thought it may have something to do with the fact that I ran `include` from a script and not the REPL, but I that wasn’t the case either. I feel like I couldn’t have imagined this behaviour - has anyone else experienced this? Or has an explanation for when and why this happens?

---

<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:** [September 15, 2020, 12:34pm UTC](https://discourse.julialang.org/t/using-include-to-bring-variables-into-scope-not-working/46636/2 "2020-09-15T12:34:26Z")

</div>

Perhaps you had `include` in a function, which indeed doesn’t work.

---

<div class="post-metadata">

**Author:** ![gobs](https://avatars.discourse-cdn.com/v4/letter/g/bc8723/32.png) [@gobs](https://discourse.julialang.org/u/gobs)\
**Post date:** [September 15, 2020, 1:00pm UTC](https://discourse.julialang.org/t/using-include-to-bring-variables-into-scope-not-working/46636/3 "2020-09-15T13:00:29Z")

</div>

Thanks! Would a loop be “equivalent” to a function? And if that’s the case, any ideas on how to circumvent that? My initial thought was to save a .jld2 file and then load it every time I needed it.

---

<div class="post-metadata">

**Author:** ![js135005](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/js135005/32/8219_2.png) [@js135005](https://discourse.julialang.org/u/js135005)\
**Post date:** [September 15, 2020, 1:17pm UTC](https://discourse.julialang.org/t/using-include-to-bring-variables-into-scope-not-working/46636/4 "2020-09-15T13:17:59Z")

</div>

That’s exactly what I do to bring a set of predefined variables including large dicts into scope.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [September 15, 2020, 1:23pm UTC](https://discourse.julialang.org/t/using-include-to-bring-variables-into-scope-not-working/46636/5 "2020-09-15T13:23:12Z")

</div>

You cannot `include` into any local scope.

If `init.jl` is defining some constant global variables, well, just do that globally. If the variables it define changes based on the caller, use a function instead.

---

<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:** [September 15, 2020, 1:25pm UTC](https://discourse.julialang.org/t/using-include-to-bring-variables-into-scope-not-working/46636/6 "2020-09-15T13:25:00Z")

</div>

> [@gobs](#):
>
> Would a loop be “equivalent” to a function? And if that’s the case, any ideas on how to circumvent that?

No, a global scope `for` loop should work. Eg

```julia
julia> @isdefined a
false

shell> cat /tmp/a.jl
a = 1

julia> for _ in 1
       include("/tmp/a.jl")
       end

julia> a
1

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [September 15, 2020, 1:35pm UTC](https://discourse.julialang.org/t/using-include-to-bring-variables-into-scope-not-working/46636/7 "2020-09-15T13:35:36Z")

</div>

> [@Tamas\_Papp](#):
>
> No, a global scope `for` loop should work. Eg

by that definition it also works in a function…

---

<div class="post-metadata">

**Author:** ![gobs](https://avatars.discourse-cdn.com/v4/letter/g/bc8723/32.png) [@gobs](https://discourse.julialang.org/u/gobs)\
**Post date:** [September 16, 2020, 6:42am UTC](https://discourse.julialang.org/t/using-include-to-bring-variables-into-scope-not-working/46636/8 "2020-09-16T06:42:18Z")

</div>

Possibly related, I noticed that if you define functions which include variables you’ve defined globally then it uses those:

```julia
julia> a = 2
2

julia> silly_func(x) = a + x
silly_func (generic function with 1 method)

julia> silly_func(2)
4

```

Personally I found this really weird and annoying. Anyway, thank you all for the clarification, I think I’m just going to start using JLD2 files…

---

<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:** [September 16, 2020, 7:04am UTC](https://discourse.julialang.org/t/using-include-to-bring-variables-into-scope-not-working/46636/9 "2020-09-16T07:04:23Z")

</div>

A good way to define parameters is to put them into a struct (or multiple structs) with `Base.@kwdef`.  
The advantages are:

- Usually no runtime costs (if struct is immutable and has concrete field types)
- Default parameters are set in the code, but can be overwritten for specific use cases (e.g. tests) when constructing the parameter instance.
- The struct can be loaded / saved the usual Julia ways

---

<div class="post-metadata">

**Author:** ![gobs](https://avatars.discourse-cdn.com/v4/letter/g/bc8723/32.png) [@gobs](https://discourse.julialang.org/u/gobs)\
**Post date:** [September 17, 2020, 1:16pm UTC](https://discourse.julialang.org/t/using-include-to-bring-variables-into-scope-not-working/46636/10 "2020-09-17T13:16:19Z")

</div>

Apologies to re-open this, but I realised that this probably relates to another problem I have sometimes.

Often I define a parameter before entering a for loop:

```julia
important_param = 5
for i = 1:5
    x = i + important_param
end

```

However, sometimes (and for reasons I haven’t been able to fathom) julia complains that `important_param` is undefined, so I have to do this:

```julia
let important_param = 5
    for i = 1:5
        x = i + important_param
    end
end

```

This literally pops out of nowhere sometimes, and I assume it’s due to me misunderstanding how scoping works in Julia. Any ideas? Or should I look at / open another thread?

---

<div class="post-metadata">

**Author:** ![gobs](https://avatars.discourse-cdn.com/v4/letter/g/bc8723/32.png) [@gobs](https://discourse.julialang.org/u/gobs)\
**Post date:** [September 18, 2020, 8:48am UTC](https://discourse.julialang.org/t/using-include-to-bring-variables-into-scope-not-working/46636/11 "2020-09-18T08:48:21Z")

</div>

I have answered my own question, having found a bunch of threads on the issue [e.g. this one](https://discourse.julialang.org/t/tips-to-cope-with-scoping-rules-of-top-level-for-loops-etc/24902). For my next project I will take the advice of writing everything in functions (though the claim that “The new debugger works like a charm” does not line up with my experience when I tried it back then). For now I’m going to keep putting `global` everywhere as if my life (or more realistically a deadline) depended on it.
