# Difference between "using" and "include"?

**URL:** <https://discourse.julialang.org/t/difference-between-using-and-include/1739>\
**Category:** General Usage\
**Created:** [January 28, 2017, 2:21pm UTC](https://discourse.julialang.org/t/difference-between-using-and-include/1739 "2017-01-28T14:21:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![phillefjonk](https://avatars.discourse-cdn.com/v4/letter/p/f04885/32.png) [@phillefjonk](https://discourse.julialang.org/u/phillefjonk)\
**Post date:** [January 28, 2017, 2:21pm UTC](https://discourse.julialang.org/t/difference-between-using-and-include/1739/1 "2017-01-28T14:21:50Z")

</div>

The documentation on Modules and the usage of “using” vs “include” is a not crystal clear I think.  
I have a main Julia script and three scripts in separate .jl files (in the same directory).

```julia
main.jl
   include("script1.jl")
   include("script2.jl")
   include("script3.jl")

<some code>
end of script

```

The code executes as I anticipate.

I think if the “include” statement as a proxy for the the code in the “sub-scripts”. I.e. the include(“script1.jl”) represents the code in the sub-scripts line by line in the main script. It is as if the code in the script1.jl is actually in the main.jl script. Is this the right way of thinking of the include statement?  
The scope of variables in the main.jl seem to include the ones in the “sub-scripts” when I run main.jl

What is then the difference of using the “using” statement vs the include statements? (I am aware of the necessity to specify the load path for modules in JULIA\_LOAD\_PATH). Is it same but different? Or same same?  
It seems to be same but different, in the sense that in a module you need to use the export statement for variables and functions that you want to be in the main scope. But in the “include” statement all variables and functions are in the main scope by default. Am I right?

---

<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:** [January 28, 2017, 2:31pm UTC](https://discourse.julialang.org/t/difference-between-using-and-include/1739/2 "2017-01-28T14:31:09Z")

</div>

> [@phillefjonk](#):
>
> Is this the right way of thinking of the include statement?

Yes

> [@phillefjonk](#):
>
> What is then the difference of using the “using” statement vs the include statements?

`using` (and `import` or `importall`) are completely different from `include`. They lookup a module in a list of predefined paths and load a module globally (which has nothing to do with the current scope) and then import bindings from that module to current scope.

---

<div class="post-metadata">

**Author:** ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)\
**Post date:** [January 28, 2017, 6:30pm UTC](https://discourse.julialang.org/t/difference-between-using-and-include/1739/3 "2017-01-28T18:30:50Z")

</div>

Julia’s `include` is a function, _not_ a simple input redirector (as in C, Fortran, or Matlab). The documentation says (emphasis mine)

> include(path::AbstractString)  
> **Evaluate** the contents of a source file in the current context.

“The current context” means the global scope of the current module when the evaluation takes place. Continuing,

> This function is typically used to load source interactively, or to combine files in packages that are broken into multiple source files.

and for those purposes the distinction usually doesn’t matter.

Another use for the textual “include” in other languages is reuse of code blocks in local contexts (where the Julia `include` function may do surprising things). A Julian pattern for this would use macros. A nice example is the `@def` macro used inside the DifferentialEquations.jl ecosystem. There, one includes a file containing `@def` macro definitions, the definitions are evaluated right then, and the macros are used to paste in blocks of code at parsing time.

---

<div class="post-metadata">

**Author:** ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)\
**Post date:** [January 29, 2017, 2:04am UTC](https://discourse.julialang.org/t/difference-between-using-and-include/1739/4 "2017-01-29T02:04:58Z")

</div>

It seems `using/import` is equivalent to `include` when loading a **file** in `LOAD_PATH`:

```julia
#test.jl in pwd()
x = 1
module Foo
  y = 2
  export y
end
######

julia> push!(LOAD_PATH,pwd())
julia> import test
WARNING: requiring "test" in module "Main" did not define a corresponding module.
julia> whos()
                          Base 34459 KB Module
                          Core 12386 KB Module
                           Foo 1194 bytes Module
                          Main 41327 KB Module
                           ans 0 bytes Void
                             x 8 bytes Int64

```

---

<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:** [January 29, 2017, 3:09am UTC](https://discourse.julialang.org/t/difference-between-using-and-include/1739/5 "2017-01-29T03:09:52Z")

</div>

This is an side effect of module loading/`eval`ing in the `Main` module, which is also where the REPL runs. Doing things other than defining a module and expect those side effects be preserved will not work in general.

---

<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:** [October 24, 2024, 2:59pm UTC](https://discourse.julialang.org/t/difference-between-using-and-include/1739/6 "2024-10-24T14:59:02Z")

</div>

A post was split to a new topic: [Difference between “using” and “include”?](https://discourse.julialang.org/t/difference-between-using-and-include/121708)
