# Loading a module outside \`Main\` module

**URL:** <https://discourse.julialang.org/t/loading-a-module-outside-main-module/15282>\
**Category:** General Usage\
**Tags:** module\
**Created:** [September 21, 2018, 2:59pm UTC](https://discourse.julialang.org/t/loading-a-module-outside-main-module/15282 "2018-09-21T14:59:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmetumutdurmus](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@ahmetumutdurmus](https://discourse.julialang.org/u/ahmetumutdurmus)\
**Post date:** [September 21, 2018, 2:59pm UTC](https://discourse.julialang.org/t/loading-a-module-outside-main-module/15282/1 "2018-09-21T14:59:53Z")

</div>

Hi,  
I am currently trying to create a module and load it back again for further use yet having trouble in doing so. Consider the trivial example:

```julia
module MyModule

export x, y
x() = "x"
y() = "y"
p() = "p"

end

using MyModule
println(x())

```

This is in convention with the documentation:  
[https://docs.julialang.org/en/v1/manual/modules/#Summary-of-module-usage-1](https://docs.julialang.org/en/v1/manual/modules/#Summary-of-module-usage-1)  
Yet when I execute that code I get the following error:

```julia
ERROR: LoadError: ArgumentError: Package MyModule not found in current path:
- Run `Pkg.add("MyModule")` to install the MyModule package.

Stacktrace:
 [1] require(::Module, ::Symbol) at .\loading.jl:817
 [2] include_string(::Module, ::String, ::String) at .\loading.jl:1002
 [3] (::getfield(Atom, Symbol("##118#123")){String,String,Module})() at C:\Users\adurmus\.julia\packages\Atom\WSz3k\src\eval.jl:120
 [4] withpath(::getfield(Atom, Symbol("##118#123")){String,String,Module}, ::String) at C:\Users\adurmus\.julia\packages\CodeTools\hB4Hy\src\utils.jl:30
 [5] withpath at C:\Users\adurmus\.julia\packages\Atom\WSz3k\src\eval.jl:46 [inlined]
 [6] #117 at C:\Users\adurmus\.julia\packages\Atom\WSz3k\src\eval.jl:117 [inlined]
 [7] hideprompt(::getfield(Atom, Symbol("##117#122")){String,String,Module}) at C:\Users\adurmus\.julia\packages\Atom\WSz3k\src\repl.jl:76
 [8] macro expansion at C:\Users\adurmus\.julia\packages\Atom\WSz3k\src\eval.jl:116 [inlined]
 [9] (::getfield(Atom, Symbol("##116#121")){Dict{String,Any}})() at .\task.jl:85
in expression starting at C:\Users\adurmus\Desktop\Julia Codes\asdsada.jl:10

```

However if I change `using MyModule` to `using Main.MyModule` I get the relevant output without no errors:  
`x`

What seems to be the problem here? What am I missing in the documentation?

Thanks a lot in advance.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [September 21, 2018, 3:00pm UTC](https://discourse.julialang.org/t/loading-a-module-outside-main-module/15282/2 "2018-09-21T15:00:53Z")

</div>

I think you need

`using .Module`

with an initial `.`

---

<div class="post-metadata">

**Author:** ![ahmetumutdurmus](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@ahmetumutdurmus](https://discourse.julialang.org/u/ahmetumutdurmus)\
**Post date:** [September 21, 2018, 3:13pm UTC](https://discourse.julialang.org/t/loading-a-module-outside-main-module/15282/3 "2018-09-21T15:13:07Z")

</div>

That does indeed fix the problem. Thank you. Do you have any idea why didn’t `using MyModule` work as intended? Is it a bug or something or rather just bad implementation by me?

---

<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:** [September 21, 2018, 3:16pm UTC](https://discourse.julialang.org/t/loading-a-module-outside-main-module/15282/4 "2018-09-21T15:16:47Z")

</div>

The docs should be updated.

`using MyModule` means to load the package `MyModule` and call `using` on that module, while `using .MyModule` simply means to call `using` on the module defined in the current module (`Main` in the REPL).

---

<div class="post-metadata">

**Author:** ![elgerson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elgerson/32/9054_2.png) [@elgerson](https://discourse.julialang.org/u/elgerson)\
**Post date:** [June 26, 2019, 12:16am UTC](https://discourse.julialang.org/t/loading-a-module-outside-main-module/15282/5 "2019-06-26T00:16:20Z")

</div>

I think I’m still missing something here. The OP didn’t specify that he was working in the REPL (in fact, it sounds like he was probably loading code from a file.) Are all modules necessarily always created as submodules of “Main”? If not, how does one go about creating a module with a top-level namespace?

---

<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:** [June 26, 2019, 7:36am UTC](https://discourse.julialang.org/t/loading-a-module-outside-main-module/15282/6 "2019-06-26T07:36:25Z")

</div>

> [@elgerson](#):
>
> The OP didn’t specify that he was working in the REPL (in fact, it sounds like he was probably loading code from a file.)

Executing things in the REPL or from a script file works the same.

> [@elgerson](#):
>
> Are all modules necessarily always created as submodules of “Main”?

If defined in a script or the REPL, yes.

> [@elgerson](#):
>
> If not, how does one go about creating a module with a top-level namespace?

A package is a top-level namespace that is loaded with `using Package`.
