Difference between "using x" vs "using .x" or "using Main.x"

I find that a little bit confusing as well. You can think like a directory structure. The packages and Main are at the top level of that structure. For the modules which are at the top level, you do not use any dot:

juila> using Main

julia> using Pkg

But when you are inside a module (we are “inside the module Main” at the REPL - correct me if this image is wrong), we can refer to the modules in this module using . (as to the files in the current directory):

julia> module Foo
         x = 1
         export x
       end
Main.Foo

Up to this point is like if Foo was a subdirectory of Main, and thus you have to refer to things within it with:

julia> Foo.x
1

julia> Main.Foo.x
1

When you do this, however:

julia> using .Foo

You bring Foo to the same level as Main.

You can use that “directory image” for understanding nested modules, for example:

julia> module Foo1
         module Faa1
           x = 1 ; export x
         end
         module Foo2
            module Faa2
              y = 1 ; export y
            end
            module Foo3
               module Faa3
                 z = 1 ; export z
               end
               using .Faa3
               using ..Faa2
               using ...Faa1
               f() = x + y + z
            end 
          end
       end
WARNING: replacing module Foo1.
Main.Foo1

julia> Foo1.Foo2.Foo3.f()
3


1 Like