# Issue with include

**URL:** <https://discourse.julialang.org/t/issue-with-include/41676>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 18, 2020, 4:12pm UTC](https://discourse.julialang.org/t/issue-with-include/41676 "2020-06-18T16:12:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkphys](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@mkphys](https://discourse.julialang.org/u/mkphys)\
**Post date:** [June 18, 2020, 4:12pm UTC](https://discourse.julialang.org/t/issue-with-include/41676/1 "2020-06-18T16:12:51Z")

</div>

I am seeing an odd issue with how include works. I am using Julia 1.0.2. Here is the simplest case that seems to cause issues:

# julia\_include\_issue.jl

```julia-auto
function f1()
    println("In f1()")
    include("params.jl")
    println("In f1(), jz=",jz)
end

function f2()
    println("In f2()")
    include("params.jl")
    println("In f2(), jz=",jz)
    jz = 0
    println("In f2(), after reset, jz=",jz)
end
f1()
f2()

```

# params.jl

```julia-auto
println("Including parameters")
jz = 1
println("In params.jl, jz=",jz)

```

# Output from running julia\_include\_issue.jl

In f1()  
Including parameters  
In params.jl, jz=1  
In f1(), jz=1  
In f2()  
Including parameters  
In params.jl, jz=1  
ERROR: LoadError: UndefVarError: jz not defined [this happens at the line in f2() where it tries to print jz]

Any thoughts on what I’m doing wrong?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [June 18, 2020, 4:18pm UTC](https://discourse.julialang.org/t/issue-with-include/41676/2 "2020-06-18T16:18:36Z")

</div>

You shouldn’t use `include` from inside functions.

---

<div class="post-metadata">

**Author:** ![mkphys](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@mkphys](https://discourse.julialang.org/u/mkphys)\
**Post date:** [June 18, 2020, 4:28pm UTC](https://discourse.julialang.org/t/issue-with-include/41676/3 "2020-06-18T16:28:13Z")

</div>

Is there a better way to do it if I want to run code from within a function (essentially just replacing the include statement with the text of the file and evaluating)?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [June 18, 2020, 4:32pm UTC](https://discourse.julialang.org/t/issue-with-include/41676/4 "2020-06-18T16:32:09Z")

</div>

What is in `params.jl`? That will help.

---

<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:** [June 18, 2020, 4:36pm UTC](https://discourse.julialang.org/t/issue-with-include/41676/5 "2020-06-18T16:36:44Z")

</div>

If the content of the file is changing and you want to observe that with multiple run of the function. No you can’t. That’s impossible. You need to find a different way to solve your **actual** problem. For example, if the things to be included is just some parameters, as the name suggested, you should just pass those in as values and you can find you favorate data storage format to load it from disk.

If the content is basically fixed and this is just a way to reuse the code, then that’s what macros for and you should use that instead. (write a macro that expands to the code you want to include). Or better, use a function if you can.
