Loading a module outside `Main` module

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:

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
Yet when I execute that code I get the following error:

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.

I think you need

using .Module

with an initial .

2 Likes

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?

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

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?

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

If defined in a script or the REPL, yes.

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