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

**URL:** <https://discourse.julialang.org/t/difference-between-using-x-vs-using-x-or-using-main-x/52269>\
**Category:** General Usage\
**Created:** [December 23, 2020, 11:26am UTC](https://discourse.julialang.org/t/difference-between-using-x-vs-using-x-or-using-main-x/52269 "2020-12-23T11:26:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [December 23, 2020, 11:26am UTC](https://discourse.julialang.org/t/difference-between-using-x-vs-using-x-or-using-main-x/52269/1 "2020-12-23T11:26:10Z")

</div>

Got curious about this behaviour:

```julia
] add DataFrames # if needed

using .DataFrames # not ok
using Main.DataFrames # not ok
using DataFrames # ok
using .DataFrames # now ok
using Main.DataFrames # now ok
module foo
end
using .foo # ok
using Main.foo # ok
using foo # NOT ok

```

Let me see if I got it… `using` when it is followed with either `Main.x` or `.x` looks for a _module_ already loaded and bring its objects into scope, otherwise it does a completely different thing: it expect a package, and use the package metadata to lookup the correct version of module x in the package, _it loads it_, and it bring its objects to scope, right ?

Edit:  
If so… what `import .x` (or `import Main.x`) does ? nothing (either the module is already loaded and it does nothing or it is not imported but it can’t be retrieved) ?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 23, 2020, 12:58pm UTC](https://discourse.julialang.org/t/difference-between-using-x-vs-using-x-or-using-main-x/52269/2 "2020-12-23T12:58:50Z")

</div>

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:

```julia
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
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
julia> Foo.x
1

julia> Main.Foo.x
1

```

When you do this, however:

```julia
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
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

```

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [December 23, 2020, 1:09pm UTC](https://discourse.julialang.org/t/difference-between-using-x-vs-using-x-or-using-main-x/52269/3 "2020-12-23T13:09:32Z")

</div>

`using X` and `import X` bring the module `X` into scope. (And `using` also brings its exported objects). Obviously, `X` must be a package recognized by your environment (say listed in the current `Project.toml`).

On the other hand, when you have defined `X` in `Main` (e.g. typing it in the REPL, as in your example), `X` is not recognized by your environment, so you have to tell the package manager that it is `Main.X` (or `.X` for short).

Never tried it, but `import .X` might be used to bring specific methods for extension into scope, e.g. `import .X: foo`.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 23, 2020, 1:13pm UTC](https://discourse.julialang.org/t/difference-between-using-x-vs-using-x-or-using-main-x/52269/4 "2020-12-23T13:13:50Z")

</div>

> [@heliosdrm](#):
>
> Never tried it, but `import .X` might be used to bring specific methods for extension into scope, e.g. `import .X: foo` .

You can do that with using as well:

```julia
julia> module Foo
         x = 1
         y = 2
         export x, y
       end
Main.Foo

julia> using .Foo:x

julia> x
1

julia> y
ERROR: UndefVarError: y not defined

```
