# Does importing a submodule do anything?

**URL:** <https://discourse.julialang.org/t/does-importing-a-submodule-do-anything/86689>\
**Category:** General Usage\
**Tags:** question, module\
**Created:** [September 2, 2022, 2:07pm UTC](https://discourse.julialang.org/t/does-importing-a-submodule-do-anything/86689 "2022-09-02T14:07:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tictaccat](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@tictaccat](https://discourse.julialang.org/u/tictaccat)\
**Post date:** [September 2, 2022, 2:07pm UTC](https://discourse.julialang.org/t/does-importing-a-submodule-do-anything/86689/1 "2022-09-02T14:07:38Z")

</div>

The following codes do the same thing:

```julia
module A
    x = 3
end
@show A.x # 3

```

and

```julia
module A
    x = 3
end
import .A
@show A.x # 3

```

Does `import .A` do anything at all in the second example? If not, why is it allowed?

---

<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:** [September 2, 2022, 2:37pm UTC](https://discourse.julialang.org/t/does-importing-a-submodule-do-anything/86689/2 "2022-09-02T14:37:03Z")

</div>

> [@tictaccat](#):
>
> Does `import .A` do anything at all in the second example? If not, why is it allowed?

No, I think it does not nothing, as it is bringing to the scope a name that is already in the current scope.

I’m not sure if that is needed in any situation, maybe it is allowed because not allowing it would require a more complicated implementation of import in general. Do you see any important pitfall on allowing it?

---

<div class="post-metadata">

**Author:** ![Matthijs\_1971](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@Matthijs\_1971](https://discourse.julialang.org/u/Matthijs_1971)\
**Post date:** [April 29, 2025, 1:49pm UTC](https://discourse.julialang.org/t/does-importing-a-submodule-do-anything/86689/3 "2025-04-29T13:49:56Z")

</div>

Reviving an old topic.

```julia
module MySubModule
    f()::Nothing = println("MySubModule.f(): Some code")

end # MySubModule

import .MySubModule # MySubModule defined in scope

MySubModule.f()

```

This works with and without the `import`. I think, just as stated above, that MySubModule is already defined in scope, so bringing it in scope with `import` again is a no-operation.

Surprisingly, I found exactly this in the [documentation](https://docs.julialang.org/en/v1/manual/modules/#Standalone-using-and-import):

```julia
In contrast,

julia> import .NiceStuff

brings only the module name into scope. 

```

Is this documentation from an era in which `import NiceStuff` made sense? Or should the documentation be updated?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [April 29, 2025, 2:12pm UTC](https://discourse.julialang.org/t/does-importing-a-submodule-do-anything/86689/4 "2025-04-29T14:12:53Z")

</div>

I suspect it’s just for consistency with the ongoing example. `NiceStuff` is an ad-hoc module defined interactively — tutorial-like — and so it is definitionally not a package that needs to be brought into scope. That could indeed be noted! Maybe something like this:

> … brings only the module name into scope. In this case, the `NiceStuff` module was interactively defined and so its name is _already_ in scope, so this doesn’t do anything here.

---

<div class="post-metadata">

**Author:** ![Matthijs\_1971](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@Matthijs\_1971](https://discourse.julialang.org/u/Matthijs_1971)\
**Post date:** [April 29, 2025, 2:23pm UTC](https://discourse.julialang.org/t/does-importing-a-submodule-do-anything/86689/5 "2025-04-29T14:23:49Z")

</div>

But if it is not defined interactively, say in a script, the same scope considerations hold, and also there the `import .SubModule` can be omitted. Does Julia have any trickery to show a list of identifiers that are in scope, I wonder now. If such a submodule is on the list, we would know a little bit more.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [April 29, 2025, 2:28pm UTC](https://discourse.julialang.org/t/does-importing-a-submodule-do-anything/86689/6 "2025-04-29T14:28:48Z")

</div>

At any top-level, you can see this with `names`:

```Julia-repl
julia> module NiceStuff
       end
Main.NiceStuff

julia> names(@ __MODULE__ )
5-element Vector{Symbol}:
 :Base
 :Core
 :InteractiveUtils
 :Main
 :NiceStuff

```
