# Proper way to include a module (Julia 1.0.1)

**URL:** <https://discourse.julialang.org/t/proper-way-to-include-a-module-julia-1-0-1/16827>\
**Category:** New to Julia\
**Created:** [October 26, 2018, 2:49pm UTC](https://discourse.julialang.org/t/proper-way-to-include-a-module-julia-1-0-1/16827 "2018-10-26T14:49:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ankneas08](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@ankneas08](https://discourse.julialang.org/u/ankneas08)\
**Post date:** [October 26, 2018, 2:49pm UTC](https://discourse.julialang.org/t/proper-way-to-include-a-module-julia-1-0-1/16827/1 "2018-10-26T14:49:45Z")

</div>

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](https://stackoverflow.com/questions/37200025/how-to-import-custom-module-in-julia) and [Julia’s Module docs](https://docs.julialang.org/en/v1/manual/modules/index.html)) would suggest that the below code should work. However, line 2 throws and error because the imported module has been imported as `Main.myModule`.

```
include("test_module.jl")
m = myStruct(1,2) > UndefVarError: myStruct not defined

```

Making either of the next changes results in expected behavior.

**Option 1:**

```
include("test_module.jl")
m = Main.myModule.myStruct(1,2)

```

**Option 2:**

```
include("test_module.jl")
using Main.myModule
m = myStruct(1,2)

```

**My Questions**

1. Am I importing the module correctly and do I have my module setup correctly? Everything that I’ve looked at suggests that I do.
2. 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?
3. What can I change in either file so that when I include the file, I have direct access to `myStruct`?

---

<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:** [October 26, 2018, 2:58pm UTC](https://discourse.julialang.org/t/proper-way-to-include-a-module-julia-1-0-1/16827/2 "2018-10-26T14:58:35Z")

</div>

There should be no need to prefix by `Main.`.

So you can do `myModule.myStruct(1,2)` or

```julia
julia> using .myModule

julia> myStruct
myStruct

```

1. Yes, looks fine.
2. Because you are calling the `include` from Main (which is the “default” module).
3. One of the points of having a `module` is to namespace the symbols in there. You could just remove the `module` completely if you want everything to happen in the same namespace as where you include it.

---

<div class="post-metadata">

**Author:** ![ankneas08](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@ankneas08](https://discourse.julialang.org/u/ankneas08)\
**Post date:** [October 26, 2018, 4:13pm UTC](https://discourse.julialang.org/t/proper-way-to-include-a-module-julia-1-0-1/16827/3 "2018-10-26T16:13:42Z")

</div>

Thanks for the reply @kristoffer.carlsson. I tried both of your suggestions though, but neither of them worked.

**Suggestion 1:**

```
include("test_module.jl")
m = myModule.myStruct(1,2) > UndefVarError: myModule not defined

```

**Suggestion 2:**

```
include("test_module.jl")
using .myModule > UndefVarError: myModule not defined
m = myStruct(1,2)

```

Did I understand your suggestions correctly?

I tried your answer to question 3 and that worked, which is good to know. I’d still like to figure out what’s going on with module imports though.

---

<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:** [October 26, 2018, 4:16pm UTC](https://discourse.julialang.org/t/proper-way-to-include-a-module-julia-1-0-1/16827/4 "2018-10-26T16:16:04Z")

</div>

```julia
shell> cat mymod.jl
module myModule
export myStruct

struct myStruct
  param1::Integer
  param2::Integer
end

end

julia> include("mymod.jl")
Main.myModule

julia> myModule.myStruct(1,2)
Main.myModule.myStruct(1, 2)

julia> using .myModule

julia> m = myStruct(1,2)
myStruct(1, 2)

```

---

<div class="post-metadata">

**Author:** ![ankneas08](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@ankneas08](https://discourse.julialang.org/u/ankneas08)\
**Post date:** [October 26, 2018, 6:30pm UTC](https://discourse.julialang.org/t/proper-way-to-include-a-module-julia-1-0-1/16827/5 "2018-10-26T18:30:03Z")

</div>

Wow, really sorry about that. I’m not sure why I got those errors initially. I’ve retried your suggestions and they all work. Thanks for the help!
