# Include mystery

**URL:** https://discourse.julialang.org/t/include-mystery/22360
**Category:** General Usage
**Created:** [March 26, 2019, 4:20pm UTC](https://discourse.julialang.org/t/include-mystery/22360 "2019-03-26T16:20:11Z")
**Posts on this page:** 12
**Page:** 1

<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: [March 26, 2019, 4:20pm UTC](https://discourse.julialang.org/t/include-mystery/22360/1 "2019-03-26T16:20:11Z")

</div>

I have two files. The first includes the second. The compiler complains about the variable `var` not being defined in the second file. Why?

First file:

```julia
function a()
	var = 1;
	include("b.jl")
end
a()

```

Second file:

```julia
@show var

```

When I run `include("a.jl")` I get:

```julia
ERROR: LoadError: LoadError: UndefVarError: var not defined               
Stacktrace:           
...

```

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [March 26, 2019, 4:23pm UTC](https://discourse.julialang.org/t/include-mystery/22360/2 "2019-03-26T16:23:22Z")

</div>

Because things are evaluated into global scope (where there is no `var`).

---

<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: [March 26, 2019, 4:28pm UTC](https://discourse.julialang.org/t/include-mystery/22360/3 "2019-03-26T16:28:11Z")

</div>

I’m not sure I follow. I thought that the compiler would simply slurp the contents of the included file.  
Evaluation of `include("a.jl")` goes through without a hitch. But then when I run, there is the error.  
I find that confusing. I thought the embedded file `b.jl` would simply become part of the code that defines the function `a`?

EDIT: Actually I meant evaluation of

```julia
function a()
	var = 1;
	include("b.jl")
end

```

This works with no problems.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [March 26, 2019, 4:31pm UTC](https://discourse.julialang.org/t/include-mystery/22360/4 "2019-03-26T16:31:07Z")

</div>

[https://docs.julialang.org/en/v1/base/base/#Base.include](https://docs.julialang.org/en/v1/base/base/#Base.include)

> Evaluate the contents of the input source file in the global scope

Include is just another function. You’re not running it by defining the function.

---

<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: [March 26, 2019, 4:31pm UTC](https://discourse.julialang.org/t/include-mystery/22360/5 "2019-03-26T16:31:36Z")

</div>

I think I’m beginning to see the light. The compiler doesn’t actually copy the contents  
of the included file into the function, but does this:

```julia
julia> @code_lowered a()                                                  
CodeInfo(                                                                 
1 ─ var = 1                                                          
│ (Main.include)("b.jl")                                           
│ %3 = Main.hey                                                         
│ value = %3                                                       
│ %5 = (Base.repr)(%3)                                                  
│ (Base.println)("hey = ", %5)                                     
└── return value                                                     
)   

```

In other words, the inclusion of the file is in the code of `a`.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [March 26, 2019, 4:31pm UTC](https://discourse.julialang.org/t/include-mystery/22360/6 "2019-03-26T16:31:58Z")

</div>

> [@PetrKryslUCSD](#):
>
> I thought that the compiler would simply slurp the contents of the included file.

What can I say, that was a wrong assumption. It parses it and evaluates the expression (and evals go into the current global scope unless given another module).

---

<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: [March 26, 2019, 4:33pm UTC](https://discourse.julialang.org/t/include-mystery/22360/7 "2019-03-26T16:33:17Z")

</div>

So is there any way I could get that behavior: I am motivated by having a piece of code that I would like to incorporate into multiple pieces “by inclusion” instead of by defining a function and calling that function?  
I suppose I am looking for a macro?

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [March 26, 2019, 4:33pm UTC](https://discourse.julialang.org/t/include-mystery/22360/8 "2019-03-26T16:33:51Z")

</div>

> [@Is "include" safe to use inside a function?](https://discourse.julialang.org/t/is-include-safe-to-use-inside-a-function/10867):
>
> Is the following safe to do? #testinclude.jl a = 2 julia\> function f() include("testinclude.jl") return a end f (generic function with 1 method) julia\> f() 2

It has a macro for this.

---

<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: [March 26, 2019, 4:36pm UTC](https://discourse.julialang.org/t/include-mystery/22360/9 "2019-03-26T16:36:07Z")

</div>

Good find. No mystery after all 🙂 Thanks.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [March 26, 2019, 4:37pm UTC](https://discourse.julialang.org/t/include-mystery/22360/10 "2019-03-26T16:37:31Z")

</div>

This misconception probably isn’t entirely your doing. Folks (maybe including myself?) have often talked about `include` as a “copy-pasting” of code when discouraging others from trying to use it as a pseudo-packaging mechanism. It may be worth trying to steer away from that mental model.

---

<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: [March 26, 2019, 4:39pm UTC](https://discourse.julialang.org/t/include-mystery/22360/11 "2019-03-26T16:39:03Z")

</div>

Indeed, you’re right. That’s the soundbite I remembered.

---

<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: [March 27, 2019, 4:41am UTC](https://discourse.julialang.org/t/include-mystery/22360/12 "2019-03-27T04:41:58Z")

</div>

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 `eval`s the contents of the file (in global scope since it is a normal function). Similarly, `include_string` could be named `eval_string`.
