Scope of a script file

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
    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)

1 Like
  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.
1 Like

Oh, the answer to #2 could have been inferred from

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 – because it creates the module “Main” (and modules always introduce global scopes)

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).


  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?
  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.