# Scope of a script file

**URL:** <https://discourse.julialang.org/t/scope-of-a-script-file/16791>\
**Category:** New to Julia\
**Tags:** scope\
**Created:** [October 25, 2018, 4:39pm UTC](https://discourse.julialang.org/t/scope-of-a-script-file/16791 "2018-10-25T16:39:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vic](https://avatars.discourse-cdn.com/v4/letter/v/9e8a1a/32.png) [@Vic](https://discourse.julialang.org/u/Vic)\
**Post date:** [October 25, 2018, 4:39pm UTC](https://discourse.julialang.org/t/scope-of-a-script-file/16791/1 "2018-10-25T16:39:32Z")

</div>

Does a script (as opposed to a module or function) introduce any scope, global or local ?

1. If used with `include("myscript")` I understood that no new scope is introduced as per [Essentials · The Julia Language](https://docs.julialang.org/en/v1.0/base/base/#Base.include)  
And neither in Repl or Jupiter notebook, I believe ?
2. What about with `julia myscript.jl` ? I guess Julia creates a new global scope in which `myscript.jl` is run?
3. (related) Is it possible to use `include("myscript")` inside of a function or other constructs, or should it be only at top-level in a module/ Repl?

(I can guess the answers to all these, but I’m not 100% certain, and maybe other beginners to Julia will benefit too)

---

<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:** [October 25, 2018, 5:06pm UTC](https://discourse.julialang.org/t/scope-of-a-script-file/16791/2 "2018-10-25T17:06:38Z")

</div>

1. Yes, no new scope.
2. Yes, it will be evaluated in a module called “Main”.
3. It is possible but the evaluation of it will happen in global scope and there are some subtleties to it. It is best to avoid.

---

<div class="post-metadata">

**Author:** ![Vic](https://avatars.discourse-cdn.com/v4/letter/v/9e8a1a/32.png) [@Vic](https://discourse.julialang.org/u/Vic)\
**Post date:** [October 25, 2018, 5:29pm UTC](https://discourse.julialang.org/t/scope-of-a-script-file/16791/3 "2018-10-25T17:29:12Z")

</div>

Oh, the answer to #2 could have been inferred from [Modules · The Julia Language](https://docs.julialang.org/en/v1.0/manual/modules/#Standard-modules-1:)

> Main is the top-level module, and Julia starts with Main set as the current module. Variables defined at the prompt go in Main, and `varinfo()` lists variables in Main.

Which also explains why “interactive REPL” is listed as construct introducing a global scope in [Scope of Variables · The Julia Language](https://docs.julialang.org/en/v1.0/manual/variables-and-scoping/#man-scope-table-1) – because it creates the module “Main” (and modules always introduce global scopes)

---

<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:** [October 25, 2018, 5:51pm UTC](https://discourse.julialang.org/t/scope-of-a-script-file/16791/4 "2018-10-25T17:51:51Z")

</div>

Technically, the REPL doesn’t create the module Main but it evaluates all its input into that module. Main exists even if the REPL is disabled (like in the `julia myfile.jl` case).

---

<div class="post-metadata">

**Author:** ![Vic](https://avatars.discourse-cdn.com/v4/letter/v/9e8a1a/32.png) [@Vic](https://discourse.julialang.org/u/Vic)\
**Post date:** [October 25, 2018, 7:54pm UTC](https://discourse.julialang.org/t/scope-of-a-script-file/16791/5 "2018-10-25T19:54:53Z")

</div>

Thanks!

1. OK, so a new Main module is created when Julia starts, either with `julia myfile.jl`, or when invoking REPL?
2. …Even if `myfile.jl` is actually another module?
3. Does that Main exist in some special file, or is it just an imaginary/abstract notion?

---

<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:** [October 25, 2018, 8:01pm UTC](https://discourse.julialang.org/t/scope-of-a-script-file/16791/6 "2018-10-25T20:01:56Z")

</div>

1. Yes, when Julia starts.
2. If `myfile.jl` contains another module then that module will just exist in `Main`. Modules can be nested.
3. It is created by Julia itself so it is not in any file.
