# What are the essential differences between \`include\` a file and \`using\` a module?

**URL:** <https://discourse.julialang.org/t/what-are-the-essential-differences-between-include-a-file-and-using-a-module/99798>\
**Category:** General Usage\
**Tags:** question, module, workflow\
**Created:** [June 3, 2023, 6:31am UTC](https://discourse.julialang.org/t/what-are-the-essential-differences-between-include-a-file-and-using-a-module/99798 "2023-06-03T06:31:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [June 3, 2023, 6:31am UTC](https://discourse.julialang.org/t/what-are-the-essential-differences-between-include-a-file-and-using-a-module/99798/1 "2023-06-03T06:31:27Z")

</div>

What are the essential differences between the following two workflows and what are their respective usage scenarios? Or in another word, what’s the necessity for writing a module instead of a plain `.jl` file?

1. Write some functions in a file named `myFile.jl`:

```julia
function sayHello()
    println("Hello, world!")
end

```

and call them in the REPL by:

```julia
include("myFile.jl")
sayHello()

```

1. Write some functions into a module in the file `myFile.jl`:

```julia
module myFile
export sayHello
function sayHello()
    println("Hello, world! ")
end
end

```

and call them in the REPL by:

```julia
push!(LOAD_PATH, ".")
using myFile
sayHello()

```

By the way, must the module name be the same as the file name?

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [June 3, 2023, 7:57am UTC](https://discourse.julialang.org/t/what-are-the-essential-differences-between-include-a-file-and-using-a-module/99798/2 "2023-06-03T07:57:12Z")

</div>

If you include a file then you are placing the function name in the global space and the function has access to any variable in the global space.

If you are “using modulename” then the function sayHello is living inside the module name space and has no access to any variables in the global space. The function only has access to variables inside the module space.

---

<div class="post-metadata">

**Author:** ![Thiago-Simoes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thiago-simoes/32/50660_2.png) [@Thiago-Simoes](https://discourse.julialang.org/u/Thiago-Simoes)\
**Post date:** [June 10, 2023, 1:41am UTC](https://discourse.julialang.org/t/what-are-the-essential-differences-between-include-a-file-and-using-a-module/99798/3 "2023-06-10T01:41:00Z")

</div>

When you import a module, your code and module are in different scopes. A variable defined inside the module is inside the scope of the module vice versa, otherwise when including a file it works as if you copy and paste the contents of the file in the place where you call `include`.

Also, a module has its own dependencies and this can help to avoid errors and isolate parts of the code.

In my personal opinion, for small things that you want to split into different files for organization, but are of the same context and purpose, using files and include is better. For separate pieces with a specific focus, I prefer to use modules, like splitting a website’s backend into three modules: Auth, DB, Services. Each module divided into files.

---

<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:** [June 10, 2023, 11:40am UTC](https://discourse.julialang.org/t/what-are-the-essential-differences-between-include-a-file-and-using-a-module/99798/4 "2023-06-10T11:40:29Z")

</div>

See my answer here: [How to structure project in julia - #44 by Henrique\_Becker](https://discourse.julialang.org/t/how-to-structure-project-in-julia/99458/44)
