# Including modules in Julia v0.7

**URL:** <https://discourse.julialang.org/t/including-modules-in-julia-v0-7/12559>\
**Category:** General Usage\
**Created:** [July 21, 2018, 1:49pm UTC](https://discourse.julialang.org/t/including-modules-in-julia-v0-7/12559 "2018-07-21T13:49:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![migarstka](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/migarstka/32/3828_2.png) [@migarstka](https://discourse.julialang.org/u/migarstka)\
**Post date:** [July 21, 2018, 1:49pm UTC](https://discourse.julialang.org/t/including-modules-in-julia-v0-7/12559/1 "2018-07-21T13:49:56Z")

</div>

I am trying to update some of my code to make it work with Julia v0.7.0-beta2.0.

I am a bit confused about what is the right way to include a bunch of modules that refer to each other.

Assume I have three files that each contain a module:

**1. Module: Types.jl**  
(a module that holds struct definitions)

```julia
module Types

export TwoNumbers

  mutable struct TwoNumbers
    a::Int64
    b::Int64
  end

end

```

**2. Module: Operations.jl**  
(a module that contains certain functions that work on the custom structs defined in Module 1)

```julia
module Operations
using Types
export switchNumbers!
  function switchNumbers!(tn::Types.TwoNumbers)
    a = copy(tn.a)
    b = copy(tn.b)

    tn.a = b
    tn.b = a
    return nothing
  end

end

```

**3. Module MainModule.jl**  
( a parent module that uses the operations from module 2)

```julia
include("./Types.jl")
include("./Operations.jl")

module MainModule
  using Operations, Types

  export performOperation

  function performOperation()
    tn = TwoNumbers(1,2)

    println("Original object: a=$(tn.a), b=$(tn.b)")
    switchNumbers!(tn)
    println("After switching: a=$(tn.a), b=$(tn.b)")
  end

end

```

Output of the following commands in **Julia 0.6.0** :

```julia
include("MainModule.jl")
MainModule.performOperations()

>>>Original object: a=1, b=2
>>>After switching: a=2, b=1

```

However, with **Julia v0.7.0-beta2.0** , I get:

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

Stacktrace:
 [1] require(::Module, ::Symbol) at ./loading.jl:816
 [2] include at ./boot.jl:317 [inlined]
 [3] include_relative(::Module, ::String) at ./loading.jl:1034
 [4] include(::Module, ::String) at ./sysimg.jl:29
 [5] include(::String) at ./client.jl:393
 [6] top-level scope at none:0
 [7] include at ./boot.jl:317 [inlined]
 [8] include_relative(::Module, ::String) at ./loading.jl:1034
 [9] include(::Module, ::String) at ./sysimg.jl:29
 [10] include(::String) at ./client.jl:393
 [11] top-level scope at none:0
in expression starting at /<PATH>/TestModules/Operations.jl:2
in expression starting at /<PATH>/TestModules/MainModule.jl:2

```

My question is: What is the best practice to include several files with modules and interdependencies into a parent module MainModule.jl (in Julia 0.7)?

---

<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:** [July 21, 2018, 1:53pm UTC](https://discourse.julialang.org/t/including-modules-in-julia-v0-7/12559/2 "2018-07-21T13:53:29Z")

</div>

Try `.Types` instead and perhaps put the includes in the main module.

---

<div class="post-metadata">

**Author:** ![migarstka](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/migarstka/32/3828_2.png) [@migarstka](https://discourse.julialang.org/u/migarstka)\
**Post date:** [July 21, 2018, 2:21pm UTC](https://discourse.julialang.org/t/including-modules-in-julia-v0-7/12559/3 "2018-07-21T14:21:45Z")

</div>

Thank you. That doesn’t seem to help.

---

<div class="post-metadata">

**Author:** ![phelipe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phelipe/32/14643_2.png) [@phelipe](https://discourse.julialang.org/u/phelipe)\
**Post date:** [July 21, 2018, 4:40pm UTC](https://discourse.julialang.org/t/including-modules-in-julia-v0-7/12559/4 "2018-07-21T16:40:24Z")

</div>

Try

```julia
include("Types.jl")
using Types
include("Operations.jl")
using Operations

```

Also see the package [https://github.com/simonster/Reexport.jl](https://github.com/simonster/Reexport.jl)  
, it can help with something.

---

<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:** [July 21, 2018, 6:47pm UTC](https://discourse.julialang.org/t/including-modules-in-julia-v0-7/12559/5 "2018-07-21T18:47:28Z")

</div>

Oh you have `using Types` in Operations.jl.

Do

```julia
using ..Types

```

in Operations.jl and

```julia
module MainModule
  include("./Types.jl")
  include("./Operations.jl")
  using .Operations, .Types
  ...
end

```

in `MainModule.jl`

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [July 21, 2018, 7:36pm UTC](https://discourse.julialang.org/t/including-modules-in-julia-v0-7/12559/6 "2018-07-21T19:36:42Z")

</div>

Previously packages were stored in `Main` and as a side effect modules defined in `Main` could be loaded as if they were packages. That is no longer the case: packages are not stored in `Main` and you cannot load modules defined in `Main` that way. In 0.7 `import X` can only be used to load actual packages (via `LOAD_PATH` etc. as described in [code loading](https://docs.julialang.org/en/stable/manual/code-loading/index.html)). Now if you want to load a module defined in `Main` from `Main` you have to load it with `import .X`; to load it from a submodule of `Main` you have to do `import ..X`.

---

<div class="post-metadata">

**Author:** ![dante](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@dante](https://discourse.julialang.org/u/dante)\
**Post date:** [January 8, 2019, 12:40pm UTC](https://discourse.julialang.org/t/including-modules-in-julia-v0-7/12559/7 "2019-01-08T12:40:48Z")

</div>

I have a follow-up question then. I have been developing code with modules in files in the same folder with the module name, where the “using TheModule” mechanism used to work. It appears that starting with Julia 0.7, I cannot find a way to load these local modules without doing `include()` at some point. The problem with this is that if `Module1` is a file and both `Module2` and `Module3` use `Module1`, I could previously just do `using` in both of those and now I have to introduce some mechanism to try and do `include` outside to avoid loading the module twice.

Am I overlooking something or is what I’m doing just an antipattern? If the latter, how should I approach the situation differently?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [January 8, 2019, 12:52pm UTC](https://discourse.julialang.org/t/including-modules-in-julia-v0-7/12559/8 "2019-01-08T12:52:39Z")

</div>

That’s the way to do it for now. There’s a plan to allow omitting the include for local imports here: [relative using/import should search current directory · Issue #4600 · JuliaLang/julia · GitHub](https://GitHub.com/JuliaLang/julia/issues/4600).

---

<div class="post-metadata">

**Author:** ![dante](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@dante](https://discourse.julialang.org/u/dante)\
**Post date:** [January 8, 2019, 1:03pm UTC](https://discourse.julialang.org/t/including-modules-in-julia-v0-7/12559/9 "2019-01-08T13:03:16Z")

</div>

Alright, thanks for the quick reply and for pointing me to that issue, I was not aware of that discussion.
