Let’s say I have a file called
test_module.jl and inside that I have defined a structure that I would like to use in other julia scripts.
module myModule export myStruct struct myStruct param1::Integer param2::Integer end end
In another file
test_include.jl, I would like to access and use the
myStruct structure. My intuition here (along with this SO question and Julia’s Module docs) would suggest that the below code should work. However, line 2 throws and error because the imported module has been imported as
include("test_module.jl") m = myStruct(1,2) > UndefVarError: myStruct not defined
Making either of the next changes results in expected behavior.
include("test_module.jl") m = Main.myModule.myStruct(1,2)
include("test_module.jl") using Main.myModule m = myStruct(1,2)
- Am I importing the module correctly and do I have my module setup correctly? Everything that I’ve looked at suggests that I do.
- Why does
include(julia_file)result in modules from that file being included in the Main namespace? Is there a way to change/control that?
- What can I change in either file so that when I include the file, I have direct access to